<?php

namespace App\Filament\Admin\Resources;

use App\Filament\Admin\Resources\CustomerResource\RelationManagers\PaymentsRelationManager;
use App\Filament\Admin\Resources\PaymentResource\Pages;
use App\Models\Payment;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables\Actions\ViewAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class PaymentResource extends Resource
{
    protected static ?string $model = Payment::class;

    protected static ?string $navigationIcon = 'lucide-hand-coins';

    protected static ?string $navigationGroup = 'Financial';

    protected static ?int $navigationSort = 2;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make([
                    Select::make('customer_id')
                        ->relationship('customer', 'company_name')
                        ->required()
                        ->searchable()
                        ->preload(),
                    TextInput::make('amount')
                        ->required()
                        ->minValue(0)
                        ->maxValue(99999999)
                        ->numeric(),
                    Textarea::make('notes'),
                ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('created_at')
                    ->label('Date')
                    ->date('Y-m-d')
                    ->searchable(),

                TextColumn::make('customer.company_name')
                    ->hidden(fn ($livewire) => $livewire::class === PaymentsRelationManager::class)
                    ->searchable(),

                TextColumn::make('notes')
                    ->limit(100)
                    ->extraHeaderAttributes(['class' => 'w-full']),

                TextColumn::make('amount')
                    ->searchable()
                    ->numeric()
                    ->money(),

                TextColumn::make('unapplied_amount')
                    ->label('Balance')
                    ->money(),
            ])
            ->actions([
                ViewAction::make(),
            ]);
    }

    public static function canAccess(): bool
    {
        return auth()->user()->is_admin;
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListPayments::route('/'),
            //            'view'  => Pages\ViewPayment::route('/{record}'),
            //            'create' => Pages\CreatePayment::route('/create'),
            //            'edit'   => Pages\EditPayment::route('/{record}/edit'),
        ];
    }
}
