<?php

namespace App\Filament\Resources\QuotaPackages;

use App\Models\QuotaPackage;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Schemas\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Actions\EditAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Forms\Components\TagsInput;
use App\Filament\Resources\QuotaPackages\Pages\ListQuotaPackages;
use App\Filament\Resources\QuotaPackages\Pages\CreateQuotaPackage;
use App\Filament\Resources\QuotaPackages\Pages\EditQuotaPackage;

class QuotaPackageResource extends Resource
{
    protected static ?string $model = QuotaPackage::class;

    // 1. PERBAIKAN IKON: Menggunakan Heroicon sesuai format sistem
    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedTicket;

    public static function getNavigationGroup(): ?string
    {
        return 'Manajemen Kuota Mitra';
    }
    public static function getNavigationSort(): ?int
    {
        return 2; // Ganti angkanya (1, 2, 3, dst) sesuai urutan yang diinginkan
    }

    public static function getModelLabel(): string
    {
        return 'Paket Kuota Properti';
    }

    // KUNCI KEAMANAN: HANYA ADMIN/SUPERADMIN YANG BISA AKSES MENU INI
    public static function canAccess(): bool
    {
        return auth()->user()?->hasRole(['super_admin', 'Admin', 'superadmin']) ?? false;
    }

    // 2. PERBAIKAN FORM: Menggunakan Schema dan ->components()
    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Detail Paket Kuota')
                    ->schema([
                        TextInput::make('name')
                            ->label('Nama Paket')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Contoh: Paket Juragan 3 Properti'),

                        TextInput::make('quota_amount')
                            ->label('Jumlah Kuota yang Didapat')
                            ->required()
                            ->numeric()
                            ->minValue(1),

                        TextInput::make('price')
                            ->label('Harga Paket')
                            ->required()
                            ->numeric()
                            ->prefix('Rp'),

                        TagsInput::make('features')
                            ->label('Fitur Tambahan (List Keterangan)')
                            ->placeholder('Ketik fitur lalu tekan Enter')
                            ->helperText('Contoh: Data kamar tidak dibatasi, Bebas biaya admin')
                            ->columnSpanFull(),

                        Toggle::make('is_active')
                            ->label('Aktifkan Paket Ini?')
                            ->default(true),
                    ])->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('Nama Paket')
                    ->searchable()
                    ->alignLeft(), // Rata kiri

                TextColumn::make('quota_amount')
                    ->label('Jumlah Kuota')
                    // Kita matikan ->badge() dan ->color(), ganti dengan kustom HTML
                    ->formatStateUsing(fn($state) => '<span style="display: inline-flex; align-items: center; justify-content: center; padding: 0.25rem 0.75rem; font-size: 0.75rem; font-weight: 600; border-radius: 9999px; color: #1d4ed8; background-color: #eff6ff; border: 1px solid #bfdbfe; width: max-content; white-space: nowrap;">' . $state . ' Slot</span>')
                    ->html() // Wajib ditambahkan agar tag HTML dirender
                    ->alignLeft()
                    ->sortable(),

                TextColumn::make('price')
                    ->label('Harga')
                    ->money('IDR')
                    ->alignLeft() // Rata kiri agar sejajar
                    ->sortable(),

                ToggleColumn::make('is_active')
                    ->label('Status Aktif')
                    ->alignCenter(), // Rata tengah agar rapi
            ])
            ->actions([
                EditAction::make(),
            ]);
    }

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