<?php

namespace App\Filament\Construction\Resources;

use App\Filament\Construction\Concerns\AuthorizesConstructionResource;
use App\Filament\Construction\Resources\ContractorResource\Pages;
use App\Models\Contractor;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class ContractorResource extends Resource
{
    use AuthorizesConstructionResource;

    protected static ?string $model = Contractor::class;

    protected static ?string $navigationIcon = 'heroicon-o-wrench-screwdriver';

    protected static ?string $navigationGroup = 'Materials & Vendors';

    protected static ?int $navigationSort = 4;

    protected static function viewPermission(): string
    {
        return 'cn_view_vendors';
    }

    protected static function managePermission(): string
    {
        return 'cn_manage_vendors';
    }

    public static function form(Form $form): Form
    {
        return $form->schema([
            Forms\Components\Section::make()->schema([
                Forms\Components\TextInput::make('name')
                    ->required()
                    ->maxLength(255),
                Forms\Components\TextInput::make('contact_person')
                    ->maxLength(255),
                Forms\Components\TextInput::make('phone')
                    ->tel()
                    ->maxLength(20),
                Forms\Components\TextInput::make('email')
                    ->email()
                    ->maxLength(255),
                Forms\Components\TextInput::make('specialty')
                    ->maxLength(255),
                Forms\Components\Select::make('project_id')
                    ->relationship('project', 'name')
                    ->searchable()
                    ->preload(),
                Forms\Components\TextInput::make('contract_amount')
                    ->numeric()
                    ->prefix('PKR')
                    ->default(0)
                    ->required(),
                Forms\Components\TextInput::make('paid_amount')
                    ->numeric()
                    ->prefix('PKR')
                    ->default(0)
                    ->required(),
            ])->columns(2),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('specialty')
                    ->searchable()
                    ->toggleable(),
                Tables\Columns\TextColumn::make('project.name')
                    ->searchable()
                    ->sortable()
                    ->placeholder('—'),
                Tables\Columns\TextColumn::make('phone')
                    ->searchable(),
                Tables\Columns\TextColumn::make('contract_amount')
                    ->money('PKR')
                    ->sortable(),
                Tables\Columns\TextColumn::make('paid_amount')
                    ->money('PKR')
                    ->sortable(),
                Tables\Columns\TextColumn::make('balance')
                    ->money('PKR')
                    ->state(fn (Contractor $record): float => (float) $record->contract_amount - (float) $record->paid_amount)
                    ->color(fn (float $state): string => $state > 0 ? 'warning' : 'success'),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('project')
                    ->relationship('project', 'name')
                    ->searchable()
                    ->preload(),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

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