<?php

namespace App\Filament\Construction\Resources;

use App\Filament\Construction\Concerns\AuthorizesConstructionResource;
use App\Filament\Construction\Resources\MaterialResource\Pages;
use App\Models\Material;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class MaterialResource extends Resource
{
    use AuthorizesConstructionResource;

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

    protected static ?string $navigationIcon = 'heroicon-o-cube';

    protected static ?string $navigationGroup = 'Materials & Vendors';

    protected static ?int $navigationSort = 1;

    protected static function viewPermission(): string
    {
        return 'cn_view_materials';
    }

    protected static function managePermission(): string
    {
        return 'cn_manage_materials';
    }

    public static function form(Form $form): Form
    {
        return $form->schema([
            Forms\Components\Section::make()->schema([
                Forms\Components\TextInput::make('name')
                    ->required()
                    ->maxLength(255),
                Forms\Components\Select::make('unit')
                    ->options([
                        'kg' => 'Kilogram (kg)',
                        'ton' => 'Ton',
                        'bag' => 'Bag',
                        'piece' => 'Piece',
                        'cubic_meter' => 'Cubic Meter',
                        'liter' => 'Liter',
                        'sqft' => 'Square Feet',
                    ])
                    ->default('kg')
                    ->required(),
                Forms\Components\Textarea::make('description')
                    ->columnSpanFull(),
            ])->columns(2),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('unit')
                    ->badge()
                    ->sortable(),
                Tables\Columns\TextColumn::make('stocks_count')
                    ->counts('stocks')
                    ->label('Stock Entries'),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('unit')
                    ->options([
                        'kg' => 'Kilogram (kg)',
                        'ton' => 'Ton',
                        'bag' => 'Bag',
                        'piece' => 'Piece',
                        'cubic_meter' => 'Cubic Meter',
                        'liter' => 'Liter',
                        'sqft' => 'Square Feet',
                    ]),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

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