<?php

namespace App\Filament\Resources\Feed\FeedPurchases\Schemas;

use App\Models\Feed;
use App\Models\FeedPurchaseItem;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\View;
use Filament\Schemas\Schema;
use Illuminate\Support\Facades\Cache;

class FeedPurchaseForm
{
    public static function configure(Schema $schema): Schema
    {
        $feeds = Cache::store('redis')->remember(
            'feeds_with_unit_for_purchase_form',
            now()->addMonth(),
            function () {
                return Feed::with('unit')
                    ->latest()
                    ->get()
                    ->map(function ($feed) {
                        $feed->image = $feed->getFirstMediaUrl('feeds');

                        return $feed;
                    });
            }
        );

        return $schema
            ->components([
                Section::make('Informasi Belanja')
                    ->schema([
                        Grid::make(3)
                            ->schema([
                                TextInput::make('reference_number')
                                    ->label('Nomor Referensi')
                                    ->placeholder('...')
                                    ->nullable()
                                    ->autocomplete(false)
                                    ->autofocus()
                                    ->helperText('Silakan masukan nomor belanja, invoice atau lainnya.'),

                                DatePicker::make('purchase_date')
                                    ->label('Tanggal Belanja')
                                    ->default(now())
                                    ->required()
                                    ->native(false)
                                    ->displayFormat('l, d F Y'),

                                TextInput::make('total_price')
                                    ->label('Total Harga')
                                    ->placeholder('0')
                                    ->readOnly()
                                    ->live()
                                    ->currencyMask(thousandSeparator: '.', decimalSeparator: ',', precision: 0)
                                    ->prefix('Rp')
                                    ->required()
                                    ->dehydrateStateUsing(fn ($state) => (int) str()->replace(',', '', (string) ($state ?? 0)))
                                    ->default(fn ($record) => (int) FeedPurchaseItem::where('feed_purchase_id', $record?->id)->sum('subtotal')),

                                Textarea::make('notes')
                                    ->label('Catatan')
                                    ->placeholder('...')
                                    ->columnSpanFull(),
                            ]),
                    ])
                    ->collapsible(),

                Grid::make([
                    'default' => 1,
                    'lg' => 3,
                ])
                    ->schema([
                        Section::make('Daftar Pakan')
                            ->schema([
                                View::make('filament.resources.manage.feed-purchases.feed-items-grid')
                                    ->viewData([
                                        'feeds' => $feeds,
                                    ]),
                            ])
                            ->columnSpan([
                                'default' => 1,
                                'lg' => 2,
                            ]),

                        Section::make('Keranjang')
                            ->schema([
                                View::make('filament.resources.manage.feed-purchases.cart')
                                    ->viewData(fn ($record) => [
                                        'cartItems' => FeedPurchaseItem::with('feed')
                                            ->where('feed_purchase_id', $record?->id)
                                            ->latest()
                                            ->get()
                                            ->map(function ($cartItem) {
                                                $cartItem->subtotal = $cartItem->unit_price * $cartItem->quantity;
                                                $cartItem->feedImage = $cartItem->feed?->getFirstMediaUrl('feeds');

                                                return $cartItem;
                                            }),
                                    ]),
                            ])
                            ->columnSpan([
                                'default' => 1,
                                'lg' => 1,
                            ]),
                    ]),
            ])
            ->columns(1);
    }
}
