<?php

namespace App\Filament\RealEstate\Resources;

use App\Enums\PropertyStatus;
use App\Filament\RealEstate\Resources\PropertyResource\Pages;
use App\Models\Property;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class PropertyResource extends Resource
{
    protected static ?string $model = Property::class;

    protected static ?string $navigationIcon = 'heroicon-o-building-office-2';

    protected static ?string $navigationGroup = 'Property Management';

    protected static ?int $navigationSort = 1;

    public static function canViewAny(): bool
    {
        return auth()->user()?->can('re_view_properties') ?? false;
    }

    public static function canCreate(): bool
    {
        return auth()->user()?->can('re_manage_properties') ?? false;
    }

    public static function canEdit($record): bool
    {
        return auth()->user()?->can('re_manage_properties') ?? false;
    }

    public static function canDelete($record): bool
    {
        return auth()->user()?->can('re_manage_properties') ?? false;
    }

    public static function getEloquentQuery(): Builder
    {
        return parent::getEloquentQuery()->with(['project']);
    }

    public static function form(Form $form): Form
    {
        return $form->schema([
            Forms\Components\Section::make()->schema([
                Forms\Components\TextInput::make('title')->required()->maxLength(255),
                Forms\Components\Select::make('type')
                    ->options([
                        'plot' => 'Plot',
                        'house' => 'House',
                        'apartment' => 'Apartment',
                        'commercial' => 'Commercial',
                        'farmhouse' => 'Farmhouse',
                    ])
                    ->required()
                    ->default('plot'),
                Forms\Components\Select::make('status')
                    ->options(collect(PropertyStatus::cases())->mapWithKeys(fn (PropertyStatus $status) => [$status->value => $status->label()]))
                    ->required()
                    ->default(PropertyStatus::Available->value),
                Forms\Components\TextInput::make('price')
                    ->numeric()
                    ->prefix('PKR')
                    ->required()
                    ->default(0),
                Forms\Components\TextInput::make('area')->maxLength(255),
                Forms\Components\TextInput::make('location')->maxLength(255),
                Forms\Components\Select::make('project_id')
                    ->relationship('project', 'name')
                    ->searchable()
                    ->preload(),
                Forms\Components\Textarea::make('description')->columnSpanFull(),
            ])->columns(2),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('title')->searchable()->sortable(),
                Tables\Columns\TextColumn::make('type')->badge()->sortable(),
                Tables\Columns\TextColumn::make('status')
                    ->badge()
                    ->formatStateUsing(fn (PropertyStatus $state): string => $state->label())
                    ->color(fn (PropertyStatus $state): string => $state->color())
                    ->sortable(),
                Tables\Columns\TextColumn::make('price')->money('PKR')->sortable(),
                Tables\Columns\TextColumn::make('area')->toggleable(),
                Tables\Columns\TextColumn::make('location')->searchable()->toggleable(),
                Tables\Columns\TextColumn::make('project.name')->label('Project')->sortable(),
                Tables\Columns\TextColumn::make('created_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('status')
                    ->options(collect(PropertyStatus::cases())->mapWithKeys(fn (PropertyStatus $status) => [$status->value => $status->label()])),
                Tables\Filters\SelectFilter::make('type')
                    ->options([
                        'plot' => 'Plot',
                        'house' => 'House',
                        'apartment' => 'Apartment',
                        'commercial' => 'Commercial',
                        'farmhouse' => 'Farmhouse',
                    ]),
                Tables\Filters\SelectFilter::make('project_id')
                    ->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\ListProperties::route('/'),
            'create' => Pages\CreateProperty::route('/create'),
            'edit' => Pages\EditProperty::route('/{record}/edit'),
        ];
    }
}
