<?php

namespace App\Filament\Tenant\Resources;

use App\Auth\Permissions;
use App\Filament\Tenant\Resources\PostResource\Pages;
use App\Models\Tenant\Post;
use App\Models\Tenant\User;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;

class PostResource extends Resource
{
    protected static ?string $model = Post::class;

    protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-wrench-screwdriver';

    protected static ?string $navigationLabel = 'Posturi de lucru';

    protected static string|\UnitEnum|null $navigationGroup = 'Admin';

    protected static ?string $modelLabel = 'pod';

    protected static ?string $pluralModelLabel = 'posturi de lucru';

    protected static ?int $navigationSort = 76;

    public static function canViewAny(): bool
    {
        return auth()->user()?->canDo(Permissions::ADMIN_SETTINGS_EDIT) ?? false;
    }

    public static function form(Schema $schema): Schema
    {
        return $schema->components([
            Schemas\Components\Section::make('Pod / Spațiu lucru')
                ->columns(2)
                ->schema([
                    Forms\Components\TextInput::make('name')
                        ->label('Nume')
                        ->required()
                        ->maxLength(80)
                        ->placeholder('Ex: Pod 1, Curte 1, Atelier electric'),
                    Forms\Components\ColorPicker::make('color')
                        ->default('#3b82f6'),
                    Forms\Components\TextInput::make('hours_per_day')
                        ->label('Ore disponibile / zi')
                        ->numeric()
                        ->step(0.5)
                        ->default(10)
                        ->helperText('Capacitatea zilnică în ore'),
                    Forms\Components\Select::make('default_master_id')
                        ->label('Mecanic implicit')
                        ->options(fn () => User::where('status', 'active')->pluck('name', 'id'))
                        ->searchable()
                        ->placeholder('Niciun mecanic implicit')
                        ->helperText('Va fi pre-completat când creezi o programare pentru acest pod'),
                    Forms\Components\TextInput::make('description')
                        ->label('Descriere')
                        ->maxLength(255)
                        ->placeholder('Ex: cu lift, fără lift, doar diagnoză...')
                        ->columnSpanFull(),
                    Forms\Components\TextInput::make('sort_order')
                        ->numeric()
                        ->default(100),
                    Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
                ]),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
                Tables\Columns\ColorColumn::make('color'),
                Tables\Columns\TextColumn::make('hours_per_day')->label('Ore/zi')->sortable(),
                Tables\Columns\TextColumn::make('defaultMaster.name')->label('Mecanic implicit')->placeholder('—'),
                Tables\Columns\TextColumn::make('description')->placeholder('—')->limit(40)->toggleable(),
                Tables\Columns\TextColumn::make('appointments_count')->counts('appointments')->label('Programări')->badge(),
                Tables\Columns\ToggleColumn::make('is_active')->label('Activ'),
            ])
            ->actions([
                Actions\EditAction::make(),
                Actions\DeleteAction::make(),
            ])
            ->defaultSort('sort_order');
    }

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