<?php

namespace App\Filament\Tenant\Resources;

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

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

    public static function getNavigationLabel(): string
    {
        return __('nav.label.Unități de măsură');
    }

    public static function getNavigationGroup(): ?string
    {
        return __('nav.group.Depozit');
    }

    protected static ?int $navigationSort = 48;

    public static function getModelLabel(): string
    {
        return __('unitate de măsură');
    }

    public static function getPluralModelLabel(): string
    {
        return __('unități de măsură');
    }

    public static function form(Schema $schema): Schema
    {
        return $schema->components([
            Schemas\Components\Section::make(__('Cod & sortare'))
                ->columns(3)
                ->schema([
                    Forms\Components\TextInput::make('code')
                        ->label(__('Cod'))
                        ->required()
                        ->maxLength(16)
                        ->helperText(__('Codul intern (buc, l, kg, ...) — stocat în DB.')),
                    Forms\Components\TextInput::make('sort_order')
                        ->label(__('Ordine'))
                        ->numeric()
                        ->default(100),
                    Forms\Components\Toggle::make('is_active')
                        ->label(__('Activ'))
                        ->default(true),
                ]),
            Schemas\Components\Section::make(__('Nume în cele 3 limbi'))
                ->columns(3)
                ->schema([
                    Forms\Components\TextInput::make('name_ro')->label('Română (RO)')->required()->maxLength(40),
                    Forms\Components\TextInput::make('name_ru')->label('Русский (RU)')->required()->maxLength(40),
                    Forms\Components\TextInput::make('name_en')->label('English (EN)')->required()->maxLength(40),
                ]),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('sort_order')->label(__('Ordine'))->sortable(),
                Tables\Columns\TextColumn::make('code')->searchable()->sortable(),
                Tables\Columns\TextColumn::make('name_ro')->label('RO')->searchable(),
                Tables\Columns\TextColumn::make('name_ru')->label('RU')->searchable(),
                Tables\Columns\TextColumn::make('name_en')->label('EN')->searchable(),
                Tables\Columns\IconColumn::make('is_active')->label(__('Activ'))->boolean(),
            ])
            ->defaultSort('sort_order')
            ->filters([
                Tables\Filters\TernaryFilter::make('is_active')->label(__('Active')),
            ])
            ->actions([
                Actions\EditAction::make(),
                Actions\DeleteAction::make(),
            ])
            ->emptyStateHeading(__('Niciun UM înregistrat'))
            ->emptyStateDescription(__('Migrarea inițială a creat setul standard. Adaugă altele dacă ai nevoie (ex: rulou, doză, cutie).'))
            ->emptyStateIcon('heroicon-o-scale');
    }

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