<?php

namespace App\Support;

final class DashboardWidgetLayout
{
    public const DASHBOARD_GRID_GAP = 'gap-3 sm:gap-4';

    /** @var array<string, string> */
    private const WIDTH_COL_CLASS = [
        'full' => 'col-span-1 md:col-span-6 xl:col-span-12',
        'two-thirds' => 'col-span-1 md:col-span-6 xl:col-span-8',
        'half' => 'col-span-1 md:col-span-6 xl:col-span-6',
        'third' => 'col-span-1 md:col-span-2 xl:col-span-4',
        'quarter' => 'col-span-1 md:col-span-2 xl:col-span-3',
    ];

    /** @var array<string, string> */
    private const HEIGHT_CELL_CLASS = [
        'compact' => 'row-span-1',
        'standard' => 'row-span-1',
        'expanded' => 'row-span-2',
    ];

    /** @var array<string, string> */
    private const HEIGHT_SURFACE_CLASS = [
        'compact' => '[&_.widget-surface]:min-h-[200px] [&_.widget-surface]:h-full',
        'standard' => '[&_.widget-surface]:min-h-[248px] [&_.widget-surface]:h-full',
        'expanded' => '[&_.widget-surface]:min-h-0 [&_.widget-surface]:h-full [&_.widget-surface]:min-h-[300px]',
    ];

    /** @var array<string, array{width: string, height: string}> */
    private const DEFAULT_WIDGET_SIZES = [
        'balance' => ['width' => 'third', 'height' => 'standard'],
        'income-expense' => ['width' => 'third', 'height' => 'standard'],
        'upcoming' => ['width' => 'third', 'height' => 'standard'],
        'subscriptions' => ['width' => 'half', 'height' => 'expanded'],
        'budgets' => ['width' => 'half', 'height' => 'expanded'],
        'recent-transactions' => ['width' => 'full', 'height' => 'expanded'],
        'goals' => ['width' => 'half', 'height' => 'expanded'],
        'debts' => ['width' => 'half', 'height' => 'expanded'],
        'ai' => ['width' => 'full', 'height' => 'expanded'],
        'safe-to-spend' => ['width' => 'third', 'height' => 'standard'],
    ];

    /** @return array{width: string, height: string} */
    public static function resolveWidgetSize(string $id, array $overrides): array
    {
        $base = self::DEFAULT_WIDGET_SIZES[$id] ?? ['width' => 'half', 'height' => 'standard'];
        $o = $overrides[$id] ?? [];

        return [
            'width' => is_string($o['width'] ?? null) && isset(self::WIDTH_COL_CLASS[$o['width']]) ? $o['width'] : $base['width'],
            'height' => is_string($o['height'] ?? null) && isset(self::HEIGHT_CELL_CLASS[$o['height']]) ? $o['height'] : $base['height'],
        ];
    }

    public static function getDashboardGridClassName(bool $editing): string
    {
        return implode(' ', array_filter([
            'grid grid-cols-1 md:grid-cols-6 xl:grid-cols-12',
            self::DASHBOARD_GRID_GAP,
            'grid-flow-dense auto-rows-[minmax(7.75rem,auto)] xl:auto-rows-[minmax(8.25rem,auto)]',
            'items-stretch',
            $editing ? 'pt-8' : '',
        ]));
    }

    /** @param  array{width: string, height: string}  $size */
    public static function getWidgetCellClassName(array $size): string
    {
        $w = $size['width'];
        $h = $size['height'];

        return (self::WIDTH_COL_CLASS[$w] ?? self::WIDTH_COL_CLASS['half'])
            .' '.(self::HEIGHT_CELL_CLASS[$h] ?? self::HEIGHT_CELL_CLASS['standard'])
            .' '.(self::HEIGHT_SURFACE_CLASS[$h] ?? self::HEIGHT_SURFACE_CLASS['standard']);
    }
}
