<?php

namespace App\Filament\Resources;

use App\Filament\Resources\BookResource\Pages;
use App\Models\Book;
use Filament\Forms;
use Filament\Tables;
use Filament\Resources\Resource;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Grid;

class BookResource extends Resource
{
    protected static ?string $model = Book::class;
    protected static ?string $navigationIcon = 'heroicon-o-book-open';
    protected static ?string $navigationLabel = 'Manajemen Koleksi';
    protected static ?string $pluralLabel = 'Koleksi Buku';
    protected static ?string $slug = 'books';
    protected static ?string $navigationGroup = '📚 Manajemen Perpustakaan';

    public static function form(Forms\Form $form): Forms\Form
    {
        return $form
            ->schema([
                Section::make('Informasi Buku')
                    ->description('Lengkapi data bibliografi koleksi perpustakaan.')
                    ->schema([
                        Grid::make(2)->schema([
                            TextInput::make('judul')
                                ->label('Judul Buku')
                                ->required()
                                ->maxLength(255),

                            TextInput::make('penulis')
                                ->label('Penulis')
                                ->maxLength(255),

                            TextInput::make('penerbit')
                                ->label('Penerbit')
                                ->maxLength(255),

                            TextInput::make('tahun_terbit')
                                ->label('Tahun Terbit')
                                ->numeric()
                                ->minValue(1900)
                                ->maxValue(date('Y')),

                            Select::make('kategori')
                                ->label('Kategori')
                                ->options([
                                    'Fiksi' => 'Fiksi',
                                    'Non-Fiksi' => 'Non-Fiksi',
                                    'Jurnal' => 'Jurnal',
                                    'Referensi' => 'Referensi',
                                    'Lainnya' => 'Lainnya',
                                ]),

                            TextInput::make('jumlah_eksemplar')
                                ->label('Jumlah Eksemplar')
                                ->numeric()
                                ->default(1)
                                ->minValue(1),
                        ]),
                    ]),
            ]);
    }

    public static function table(Tables\Table $table): Tables\Table
    {
        return $table
            ->columns([
                TextColumn::make('judul')->label('Judul Buku')->searchable()->sortable(),
                TextColumn::make('penulis')->label('Penulis')->sortable(),
                TextColumn::make('penerbit')->label('Penerbit')->sortable(),
                TextColumn::make('tahun_terbit')->label('Tahun'),
                TextColumn::make('kategori')->label('Kategori')->badge(),
                TextColumn::make('jumlah_eksemplar')->label('Stok')->color('success'),
                TextColumn::make('created_at')->label('Ditambahkan')->date(),
            ])
            ->defaultSort('created_at', 'desc');
    }

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

