<?php

namespace App\Filament\Resources\TransactionResource\Pages;

use App\Filament\Resources\TransactionResource;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Pages\EditRecord;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

/**
 * Ported from TransactionController::update(): only the mutable payment fields
 * are editable (amount, method, taken_on, reference, note, cheque details).
 * Customer/branch/allocation are fixed at creation — matching the source edit
 * form, which submits exactly this field set. Gated by `isms_transactions.approve`
 * (the source edit route's `can_do`).
 */
class EditTransaction extends EditRecord
{
    protected static string $resource = TransactionResource::class;

    public function form(Schema $schema): Schema
    {
        return $schema->components([
            Section::make('Payment')->schema([
                Select::make('payment_method_id')
                    ->label('Payment method')
                    ->relationship('paymentMethod', 'name', modifyQueryUsing: fn ($query) => $query->where('is_active', true))
                    ->searchable()
                    ->preload()
                    ->required(),
                TextInput::make('amount')
                    ->numeric()->required()->minValue(0.01)->maxValue(99999999.99)->prefix('Rs.')
                    // Corrections happen against a known customer — show what
                    // they still owe so a revised figure is checked, not guessed.
                    ->hint(function (): ?string {
                        $customerId = (int) ($this->record?->customer_id ?? 0);
                        $outstanding = TransactionResource::customerOutstanding($customerId);

                        return $outstanding > 0
                            ? 'Outstanding: Rs. '.number_format($outstanding, 2)
                            : null;
                    }),
                DatePicker::make('taken_on')
                    ->label('Taken on')
                    ->helperText('Leave blank to use the current time.'),
                TextInput::make('reference')->maxLength(100),
            ])->columns(3),

            Section::make('Cheque details')
                ->visible(fn (callable $get): bool => TransactionResource::isChequeMethod($get('payment_method_id')))
                ->schema([
                    TextInput::make('cheque_number')->maxLength(50),
                    TextInput::make('cheque_bank')->maxLength(100),
                    DatePicker::make('cheque_date'),
                ])->columns(3),

            Section::make('Note')->schema([
                Textarea::make('note')->maxLength(500)->columnSpanFull(),
            ]),
        ]);
    }

    protected function getRedirectUrl(): string
    {
        return TransactionResource::getUrl('view', ['record' => $this->record]);
    }
}
