<?php

namespace App\Filament\Tenant\Resources;

use App\Filament\Tenant\Resources\CallResource\Pages;
use App\Models\Tenant\Call;
use App\Models\Tenant\Client;
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 CallResource extends Resource
{
    protected static ?string $model = Call::class;

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

    protected static ?string $navigationLabel = 'Apeluri';

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

    protected static ?string $modelLabel = 'apel';

    protected static ?string $pluralModelLabel = 'apeluri';

    protected static ?int $navigationSort = 62;

    public static function form(Schema $schema): Schema
    {
        return $schema->components([
            Schemas\Components\Section::make('Apel')
                ->columns(2)
                ->schema([
                    Forms\Components\DateTimePicker::make('called_at')->label('Data & ora')->default(now())->required(),
                    Forms\Components\Select::make('direction')
                        ->options(Call::DIRECTIONS)
                        ->default('incoming')
                        ->required(),
                    Forms\Components\TextInput::make('phone')->label('Telefon')->tel()->required()->maxLength(40),
                    Forms\Components\Select::make('status')
                        ->options(Call::STATUSES)
                        ->default('answered')
                        ->required(),
                    Forms\Components\TextInput::make('duration_sec')->label('Durată (sec)')->numeric()->default(0),
                    Forms\Components\Select::make('client_id')
                        ->label('Client')
                        ->options(fn () => Client::pluck('name', 'id'))
                        ->searchable(),
                ]),
            Forms\Components\Textarea::make('notes')->label('Notițe')->columnSpanFull()->rows(2),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('called_at')->label('Data')->dateTime('d.m.Y H:i')->sortable(),
                Tables\Columns\TextColumn::make('direction')
                    ->formatStateUsing(fn ($s) => Call::DIRECTIONS[$s] ?? $s)
                    ->badge()
                    ->colors([
                        'success' => ['incoming'],
                        'info' => ['outgoing'],
                        'danger' => ['missed'],
                    ]),
                Tables\Columns\TextColumn::make('phone')->copyable()->searchable(),
                Tables\Columns\TextColumn::make('client.name')->label('Client')->placeholder('—'),
                Tables\Columns\TextColumn::make('duration_formatted')->label('Durată')->state(fn (Call $r) => $r->duration_formatted),
                Tables\Columns\TextColumn::make('status')
                    ->formatStateUsing(fn ($s) => Call::STATUSES[$s] ?? $s)
                    ->badge(),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('direction')->options(Call::DIRECTIONS),
                Tables\Filters\Filter::make('today')
                    ->label('Astăzi')
                    ->query(fn ($q) => $q->whereDate('called_at', today())),
                Tables\Filters\Filter::make('missed')
                    ->label('Pierdute')
                    ->query(fn ($q) => $q->where('direction', 'missed')->orWhere('status', 'missed')),
            ])
            ->actions([
                Actions\EditAction::make(),
                Actions\DeleteAction::make(),
            ])
            ->defaultSort('called_at', 'desc');
    }

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