<?php

namespace App\Filament\Admin\Resources;

use App\Filament\Admin\Resources\FirmResource\Pages;
use App\Models\Firm;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class FirmResource extends Resource
{
    protected static ?string $model = Firm::class;
    protected static ?string $navigationIcon = 'heroicon-o-building-office-2';
    protected static ?string $navigationGroup = 'Settings';

    public static function form(Form $form): Form
    {
        return $form->schema([
            Forms\Components\Section::make('Firm Identity')->schema([
                Forms\Components\TextInput::make('name')->required()->maxLength(255),
                Forms\Components\TextInput::make('legal_name')->nullable(),
                Forms\Components\Textarea::make('address')->rows(3)->nullable(),
                Forms\Components\TextInput::make('country')->nullable(),
                Forms\Components\Toggle::make('is_active')->default(true),
            ])->columns(2),

            Forms\Components\Section::make('Banking Details')->schema([
                Forms\Components\TextInput::make('bank_name')->nullable(),
                Forms\Components\TextInput::make('bank_account_number')->nullable(),
                Forms\Components\TextInput::make('swift_code')->maxLength(20)->nullable(),
                Forms\Components\TextInput::make('iban')->maxLength(50)->nullable(),
            ])->columns(2),

            Forms\Components\Section::make('Branding')->schema([
                Forms\Components\FileUpload::make('logo')
                    ->image()
                    ->directory('firms/logos')
                    ->nullable(),
            ]),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
                Tables\Columns\TextColumn::make('legal_name')->searchable()->toggleable(),
                Tables\Columns\TextColumn::make('bank_name')->toggleable(),
                Tables\Columns\IconColumn::make('is_active')->boolean(),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index'  => Pages\ListFirms::route('/'),
            'create' => Pages\CreateFirm::route('/create'),
            'edit'   => Pages\EditFirm::route('/{record}/edit'),
        ];
    }
}
