<?php

declare(strict_types=1);

namespace Maestro\Core\DataTables;

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Request;

/**
 * Column metadata for list views that use {@code B2CDataTable} + {@code tableKey}
 * but do not yet expose {@see DataTableResource} POST /fetch (custom REST lists).
 * Used for Superadmin default columns + {@see DataTableController::columns} fallback.
 */
final class ExtraDatatableColumnDefinitions
{
    /**
     * @return list<string>
     */
    public static function keys(): array
    {
        return ['offers', 'invoices'];
    }

    public static function has(string $resourceKey): bool
    {
        return in_array($resourceKey, self::keys(), true);
    }

    public static function authorize(Request $request, string $resourceKey): void
    {
        if (! self::has($resourceKey)) {
            throw new AuthorizationException('Unknown resource.');
        }

        $user = $request->user();

        if ($resourceKey === 'offers') {
            if (! $user?->can('offers.view')) {
                throw new AuthorizationException('You do not have permission to view offers.');
            }

            return;
        }

        if ($resourceKey === 'invoices') {
            if (! $user?->can('invoices.view')) {
                throw new AuthorizationException('You do not have permission to view invoices.');
            }
        }
    }

    /**
     * @return array<int, array<string, mixed>>
     */
    public static function columns(string $resourceKey): array
    {
        return match ($resourceKey) {
            'offers' => [
                [
                    'key' => 'offer_number',
                    'type' => 'text',
                    'sortable' => true,
                    'searchable' => true,
                    'filterable' => false,
                    'label' => __('maestro::datatable.columns.offers.offer_number'),
                    'defaultVisible' => true,
                ],
                [
                    'key' => 'subject',
                    'type' => 'text',
                    'sortable' => true,
                    'searchable' => true,
                    'filterable' => false,
                    'label' => __('maestro::datatable.columns.offers.subject'),
                    'defaultVisible' => true,
                ],
                [
                    'key' => 'status',
                    'type' => 'text',
                    'sortable' => true,
                    'searchable' => false,
                    'filterable' => false,
                    'label' => __('maestro::datatable.columns.offers.status'),
                    'defaultVisible' => true,
                ],
                [
                    'key' => 'valid_until',
                    'type' => 'text',
                    'sortable' => true,
                    'searchable' => false,
                    'filterable' => false,
                    'label' => __('maestro::datatable.columns.offers.valid_until'),
                    'defaultVisible' => true,
                ],
                [
                    'key' => 'updated_at',
                    'type' => 'date',
                    'sortable' => true,
                    'searchable' => false,
                    'filterable' => false,
                    'label' => __('maestro::datatable.columns.offers.updated_at'),
                    'defaultVisible' => false,
                ],
            ],
            'invoices' => [
                [
                    'key' => 'document_number',
                    'type' => 'text',
                    'sortable' => true,
                    'searchable' => true,
                    'filterable' => true,
                    'label' => __('maestro::datatable.columns.invoices.document_number'),
                    'defaultVisible' => true,
                ],
                [
                    'key' => 'type',
                    'type' => 'text',
                    'sortable' => false,
                    'searchable' => false,
                    'filterable' => true,
                    'label' => __('maestro::datatable.columns.invoices.type'),
                    'defaultVisible' => true,
                ],
                [
                    'key' => 'customer_name',
                    'type' => 'text',
                    'sortable' => false,
                    'searchable' => true,
                    'filterable' => false,
                    'label' => __('maestro::datatable.columns.invoices.customer_name'),
                    'defaultVisible' => true,
                ],
                [
                    'key' => 'invoice_date',
                    'type' => 'date',
                    'sortable' => true,
                    'searchable' => false,
                    'filterable' => false,
                    'label' => __('maestro::datatable.columns.invoices.invoice_date'),
                    'defaultVisible' => true,
                ],
                [
                    'key' => 'total_inc_vat',
                    'type' => 'text',
                    'sortable' => true,
                    'searchable' => false,
                    'filterable' => false,
                    'label' => __('maestro::datatable.columns.invoices.total_inc_vat'),
                    'defaultVisible' => true,
                ],
                [
                    'key' => 'status',
                    'type' => 'text',
                    'sortable' => true,
                    'searchable' => false,
                    'filterable' => true,
                    'label' => __('maestro::datatable.columns.invoices.status'),
                    'defaultVisible' => true,
                ],
            ],
            default => [],
        };
    }
}
