<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpInventoryBatches\RelationManagers;

use App\Filament\Resources\DberpInventorySerialNumbers\DberpInventorySerialNumberResource;
use App\Models\DberpInventorySerialNumber;
use Filament\Actions\ViewAction;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

class InventoryBatchSerialNumbersRelationManager extends RelationManager
{
    protected static string $relationship = 'serialNumbers';

    public static function getTitle(Model $ownerRecord, string $pageClass): string
    {
        return __('dberp.relations.inventory_batch_serial_numbers');
    }

    protected static function getModelLabel(): ?string
    {
        return __('dberp.resources.inventory_serial_number.label');
    }

    protected static function getPluralModelLabel(): ?string
    {
        return __('dberp.resources.inventory_serial_number.plural');
    }

    public function form(Schema $schema): Schema
    {
        return $schema;
    }

    public function table(Table $table): Table
    {
        return $table
            ->defaultSort('serial_no')
            ->columns([
                TextColumn::make('serial_no')->label(__('dberp.fields.serial_no'))->searchable(),
                TextColumn::make('status')->label(__('dberp.fields.serial_number_status'))->badge(),
                TextColumn::make('warehouse.name')->label(__('dberp.fields.warehouse')),
                ToggleColumn::make('is_enabled')
                    ->label(__('dberp.fields.is_enabled')),
            ])
            ->recordActions([
                ViewAction::make()
                    ->visible(fn (DberpInventorySerialNumber $record): bool => DberpInventorySerialNumberResource::canView($record))
                    ->url(fn (DberpInventorySerialNumber $record): string => DberpInventorySerialNumberResource::getUrl('view', ['record' => $record])),
            ])
            ->paginated([10, 25, 50]);
    }
}
