<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Filament\Resources\DberpProductBrands;

use App\Filament\Concerns\InteractsWithDberpPermissions;
use App\Filament\Resources\DberpProductBrands\Pages\CreateDberpProductBrand;
use App\Filament\Resources\DberpProductBrands\Pages\EditDberpProductBrand;
use App\Filament\Resources\DberpProductBrands\Pages\ListDberpProductBrands;
use App\Models\DberpProductBrand;
use App\Services\DberpPermissionService;
use BackedEnum;
use Filament\Actions\DeleteAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
use UnitEnum;

class DberpProductBrandResource extends Resource
{
    use InteractsWithDberpPermissions;

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

    protected static ?string $recordTitleAttribute = 'name';

    protected static ?int $navigationSort = 25;

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make(__('dberp.sections.master_basic'))
                    ->schema([
                        TextInput::make('brand_code')
                            ->label(__('dberp.fields.brand_code'))
                            ->maxLength(64)
                            ->nullable()
                            ->unique(ignoreRecord: true)
                            ->helperText(__('dberp.hints.product_brand_code_auto'))
                            ->validationMessages([
                                'unique' => (string) __('dberp.validation.product_brand_code_unique'),
                            ]),
                        TextInput::make('name')
                            ->label(__('dberp.fields.brand_profile_name'))
                            ->required()
                            ->maxLength(120)
                            ->unique(ignoreRecord: true)
                            ->validationMessages([
                                'unique' => (string) __('dberp.validation.product_brand_name_unique'),
                            ]),
                        TextInput::make('short_name')
                            ->label(__('dberp.fields.brand_short_name'))
                            ->maxLength(64),
                        TextInput::make('logo')
                            ->label(__('dberp.fields.brand_logo'))
                            ->maxLength(512)
                            ->helperText(__('dberp.hints.brand_logo')),
                        TextInput::make('sort_order')
                            ->label(__('dberp.fields.sort_order'))
                            ->numeric()
                            ->default(0)
                            ->required()
                            ->helperText(__('dberp.hints.brand_sort')),
                        Toggle::make('is_enabled')
                            ->label(__('dberp.fields.brand_is_enabled'))
                            ->default(true),
                        Textarea::make('description')
                            ->label(__('dberp.fields.brand_description'))
                            ->rows(3)
                            ->columnSpanFull(),
                    ])
                    ->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->defaultSort('sort_order', 'asc')
            ->columns([
                TextColumn::make('brand_code')
                    ->label(__('dberp.fields.brand_code'))
                    ->searchable(),
                TextColumn::make('name')
                    ->label(__('dberp.fields.brand_profile_name'))
                    ->searchable(),
                TextColumn::make('short_name')
                    ->label(__('dberp.fields.brand_short_name'))
                    ->toggleable(),
                TextColumn::make('logo')
                    ->label(__('dberp.fields.brand_logo'))
                    ->limit(32)
                    ->toggleable(isToggledHiddenByDefault: true),
                ToggleColumn::make('is_enabled')
                    ->label(__('dberp.fields.brand_is_enabled')),
                TextColumn::make('sort_order')
                    ->label(__('dberp.fields.sort_order'))
                    ->sortable(),
                TextColumn::make('updated_at')
                    ->label(__('dberp.fields.updated_at'))
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                TernaryFilter::make('is_enabled')
                    ->label(__('dberp.fields.brand_is_enabled')),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ]);
    }

    public static function canViewAny(): bool
    {
        return static::allowsPermission(DberpPermissionService::VIEW_PRODUCT_BRAND);
    }

    public static function canView(Model $record): bool
    {
        return static::canViewAny();
    }

    public static function canCreate(): bool
    {
        return static::allowsPermission(DberpPermissionService::CREATE_PRODUCT_BRAND);
    }

    public static function canEdit(Model $record): bool
    {
        return static::allowsPermission(DberpPermissionService::EDIT_PRODUCT_BRAND);
    }

    public static function canDelete(Model $record): bool
    {
        return static::allowsPermission(DberpPermissionService::DELETE_PRODUCT_BRAND);
    }

    public static function getPages(): array
    {
        return [
            'index' => ListDberpProductBrands::route('/'),
            'create' => CreateDberpProductBrand::route('/create'),
            'edit' => EditDberpProductBrand::route('/{record}/edit'),
        ];
    }

    public static function getNavigationGroup(): string|UnitEnum|null
    {
        return __('dberp.nav.product');
    }

    public static function getNavigationIcon(): string|BackedEnum|Htmlable|null
    {
        return null;
    }

    public static function getModelLabel(): string
    {
        return __('dberp.resources.product_brand.label');
    }

    public static function getPluralModelLabel(): string
    {
        return __('dberp.resources.product_brand.plural');
    }
}
