<?php

namespace App\Filament\App\Resources\OpeningBalances;

use App\Filament\App\Resources\AppResource;
use App\Filament\App\Resources\OpeningBalances\Pages\ManageOpeningBalances;
use App\Models\Account;
use App\Models\OpeningBalance;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ViewAction;
use Filament\Facades\Filament;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use UnitEnum;

class OpeningBalanceResource extends AppResource
{
    protected static ?string $model = OpeningBalance::class;

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;

    protected static string|UnitEnum|null $navigationGroup = 'Settings';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Hidden::make('user_id')
                    ->default(fn (): ?int => Filament::auth()->id()),
                Select::make('account_id')
                    ->label('Account')
                    ->options(fn (): array => Account::query()
                        ->where('user_id', Filament::auth()->id())
                        ->pluck('name', 'id')
                        ->all()),
                TextInput::make('amount')
                    ->numeric()
                    ->required(),
                DatePicker::make('effective_date')
                    ->required(),
                Textarea::make('note')
                    ->rows(3),
            ]);
    }

    public static function infolist(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextEntry::make('account.name')->label('Account'),
                TextEntry::make('amount'),
                TextEntry::make('effective_date')->date(),
                TextEntry::make('note'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('account.name')->label('Account'),
                TextColumn::make('amount')->money('INR'),
                TextColumn::make('effective_date')->date()->sortable(),
                TextColumn::make('note')->limit(30),
            ])
            ->filters([
                //
            ])
            ->recordActions([
                ViewAction::make(),
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => ManageOpeningBalances::route('/'),
        ];
    }
}
