<?php

namespace App\Filament\Resources;

use App\Filament\Exports\BranchExporter;
use App\Filament\Resources\BranchResource\Pages\ListBranches;
use App\Filament\Resources\Concerns\HasModulePermissions;
use App\Filament\Resources\Concerns\HasStandardTableFeatures;
use App\Models\Branch;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Enums\FiltersLayout;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

/**
 * Ported from App\Http\Controllers\Isms\BranchController (docs/migration-plan.md
 * Phase 0 smoke test / Phase 1 seed). List-only, matching the source app
 * exactly: routes/web.php registers only BranchController@index on the
 * tenant side (isms_branches.access) — create/edit/delete exist on the
 * controller and in the permission catalog (isms_branches.create/.edit/.delete)
 * but are NOT reachable by any tenant-side route; all branch mutation happens
 * on the central/landlord panel via Central\BranchManagementController
 * (Phase 10). See Decisions Log for this discovery — deliberately not
 * completing the dead create/edit/delete code path here without agreement.
 */
class BranchResource extends Resource
{
    use HasModulePermissions;
    use HasStandardTableFeatures;

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

    protected static string $permissionModuleKey = 'isms_branches';

    protected static string $exporter = BranchExporter::class;

    protected static string|\BackedEnum|null $navigationIcon = Heroicon::OutlinedBuildingStorefront;

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

    protected static ?string $navigationLabel = 'Branches';

    protected static ?int $navigationSort = 70;

    public static function form(Schema $schema): Schema
    {
        return $schema->components([
            Section::make()->schema([]),
        ]);
    }

    public static function table(Table $table): Table
    {
        return static::standardTableDefaults($table)
            ->recordUrl(static::standardRecordUrl())
            ->emptyStateHeading('No branches yet')
            ->emptyStateDescription('Add a branch to start organising tills and stock.')
            ->columns([
                TextColumn::make('name')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('phone')
                    ->toggleable(),
                TextColumn::make('email')
                    ->toggleable(),
                TextColumn::make('city')
                    ->toggleable(),
                IconColumn::make('is_active')
                    ->boolean()
                    ->label('Active'),
            ])
            ->filters([
                Filter::make('name')
                    ->schema([TextInput::make('value')->label('Name')])
                    ->query(fn (Builder $query, array $data): Builder => $query->when(
                        $data['value'] ?? null,
                        fn (Builder $q, $value) => $q->where('name', 'like', "%{$value}%"),
                    )),
                Filter::make('city')
                    ->schema([TextInput::make('value')->label('City')])
                    ->query(fn (Builder $query, array $data): Builder => $query->when(
                        $data['value'] ?? null,
                        fn (Builder $q, $value) => $q->where('city', 'like', "%{$value}%"),
                    )),
                TernaryFilter::make('is_active')->label('Active'),
            ])
            ->filtersLayout(FiltersLayout::AboveContentCollapsible)
            ->filtersFormColumns(2)
            ->defaultSort('is_active', 'desc')
            ->recordActions([])
            ->headerActions(static::standardExportActions())
            ->toolbarActions(static::standardExportBulkActions());
    }

    public static function getPages(): array
    {
        return [
            'index' => ListBranches::route('/'),
        ];
    }
}
