<?php

declare(strict_types=1);

namespace App\Admin;

/**
 * Declarative CRUD module definitions. Each module drives the generic
 * list/create/edit/delete admin screens.
 *
 * Field types: text, textarea, richtext, number, checkbox, date, datetime,
 *              image (URL + upload widget), json (raw JSON textarea),
 *              select (static options), fk (foreign key dropdown).
 */
final class Modules
{
    /** @return array<string, array<string, mixed>> */
    public static function all(): array
    {
        return [
            'treatments' => [
                'label'   => 'Treatments',
                'table'   => 'treatments',
                'icon'    => '💆',
                'order'   => 'sort_order, id',
                'columns' => ['name', 'duration', 'price', 'featured', 'status'],
                'fields'  => [
                    'name'        => ['type' => 'text', 'required' => true],
                    'slug'        => ['type' => 'text', 'required' => true, 'help' => 'URL identifier, e.g. wadadli-massage'],
                    'category_id' => ['type' => 'fk', 'table' => 'treatment_categories', 'label' => 'Category'],
                    'description' => ['type' => 'textarea', 'required' => true],
                    'duration'    => ['type' => 'text', 'required' => true],
                    'price'       => ['type' => 'number', 'required' => true, 'step' => '0.01'],
                    'image'       => ['type' => 'image'],
                    'featured'    => ['type' => 'checkbox', 'help' => 'Show on the homepage'],
                    'status'      => ['type' => 'checkbox', 'label' => 'Visible on website', 'default' => 1],
                    'sort_order'  => ['type' => 'number', 'default' => 0],
                ],
            ],
            'treatment_categories' => [
                'label'   => 'Treatment Categories',
                'table'   => 'treatment_categories',
                'icon'    => '🏷️',
                'hidden'  => true,
                'columns' => ['name'],
                'fields'  => ['name' => ['type' => 'text', 'required' => true]],
            ],
            'packages' => [
                'label'   => 'Packages',
                'table'   => 'packages',
                'icon'    => '🌴',
                'order'   => 'sort_order, id',
                'columns' => ['name', 'tag', 'price', 'active'],
                'fields'  => [
                    'name'        => ['type' => 'text', 'required' => true],
                    'slug'        => ['type' => 'text', 'required' => true],
                    'tag'         => ['type' => 'text', 'help' => 'Badge text, e.g. dates'],
                    'dates_label' => ['type' => 'text'],
                    'description' => ['type' => 'textarea', 'required' => true],
                    'included'    => ['type' => 'json', 'shape' => 'list', 'help' => 'One item per line, e.g. Accommodation'],
                    'price'       => ['type' => 'number', 'required' => true, 'step' => '0.01'],
                    'price_label' => ['type' => 'text'],
                    'promo_price' => ['type' => 'number', 'step' => '0.01'],
                    'promo_label' => ['type' => 'text'],
                    'image'       => ['type' => 'image'],
                    'active'      => ['type' => 'checkbox', 'default' => 1],
                    'sort_order'  => ['type' => 'number', 'default' => 0],
                ],
            ],
            'gallery_images' => [
                'label'   => 'Gallery',
                'table'   => 'gallery_images',
                'icon'    => '🖼️',
                'order'   => 'sort_order, id',
                'columns' => ['src', 'alt', 'size', 'active'],
                'fields'  => [
                    'gallery_id' => ['type' => 'fk', 'table' => 'gallery', 'label' => 'Album / Category'],
                    'src'        => ['type' => 'image', 'required' => true, 'label' => 'Image'],
                    'alt'        => ['type' => 'text', 'label' => 'Alt text', 'required' => true],
                    'size'       => ['type' => 'select', 'options' => ['' => 'Normal', 'tall' => 'Tall', 'wide' => 'Wide']],
                    'active'     => ['type' => 'checkbox', 'default' => 1],
                    'sort_order' => ['type' => 'number', 'default' => 0],
                ],
            ],
            'gallery' => [
                'label'   => 'Gallery Albums',
                'table'   => 'gallery',
                'icon'    => '📁',
                'hidden'  => true,
                'columns' => ['name'],
                'fields'  => ['name' => ['type' => 'text', 'required' => true]],
            ],
            'testimonials' => [
                'label'   => 'Testimonials',
                'table'   => 'testimonials',
                'icon'    => '⭐',
                'order'   => 'sort_order, id',
                'columns' => ['client_name', 'location', 'rating', 'approved'],
                'fields'  => [
                    'client_name' => ['type' => 'text', 'required' => true],
                    'location'    => ['type' => 'text'],
                    'review'      => ['type' => 'textarea', 'required' => true],
                    'rating'      => ['type' => 'select', 'options' => ['5' => '★★★★★', '4' => '★★★★', '3' => '★★★', '2' => '★★', '1' => '★'], 'default' => '5'],
                    'photo'       => ['type' => 'image'],
                    'date'        => ['type' => 'date'],
                    'approved'    => ['type' => 'checkbox', 'default' => 1],
                    'sort_order'  => ['type' => 'number', 'default' => 0],
                ],
            ],
            'team_members' => [
                'label'   => 'Team',
                'table'   => 'team_members',
                'icon'    => '👥',
                'order'   => 'sort_order, id',
                'columns' => ['name', 'position', 'active'],
                'fields'  => [
                    'name'       => ['type' => 'text', 'required' => true],
                    'position'   => ['type' => 'text', 'required' => true],
                    'bio'        => ['type' => 'textarea'],
                    'photo'      => ['type' => 'image'],
                    'socials'    => ['type' => 'json', 'help' => 'e.g. {"instagram":"https://...","facebook":"https://..."}'],
                    'active'     => ['type' => 'checkbox', 'default' => 1],
                    'sort_order' => ['type' => 'number', 'default' => 0],
                ],
            ],
            'blogs' => [
                'label'   => 'Blog',
                'table'   => 'blogs',
                'icon'    => '📝',
                'order'   => 'published_at DESC',
                'columns' => ['title', 'status', 'published_at'],
                'fields'  => [
                    'title'            => ['type' => 'text', 'required' => true],
                    'slug'             => ['type' => 'text', 'required' => true],
                    'category_id'      => ['type' => 'fk', 'table' => 'blog_categories', 'label' => 'Category'],
                    'excerpt'          => ['type' => 'textarea'],
                    'body'             => ['type' => 'richtext', 'required' => true],
                    'image'            => ['type' => 'image', 'label' => 'Featured image'],
                    'tags'             => ['type' => 'json', 'shape' => 'list', 'help' => 'One tag per line, e.g. wellness'],
                    'status'           => ['type' => 'select', 'options' => ['draft' => 'Draft', 'published' => 'Published'], 'default' => 'draft'],
                    'published_at'     => ['type' => 'datetime', 'help' => 'Future date = scheduled publishing'],
                    'meta_title'       => ['type' => 'text', 'help' => 'SEO title (optional)'],
                    'meta_description' => ['type' => 'textarea', 'help' => 'SEO description (optional)'],
                ],
            ],
            'blog_categories' => [
                'label'   => 'Blog Categories',
                'table'   => 'blog_categories',
                'icon'    => '🏷️',
                'hidden'  => true,
                'columns' => ['name'],
                'fields'  => ['name' => ['type' => 'text', 'required' => true]],
            ],
            'bookings' => [
                'label'     => 'Bookings',
                'table'     => 'bookings',
                'icon'      => '💳',
                'order'     => 'created_at DESC, id DESC',
                'canCreate' => false,
                'columns'   => ['reference', 'name', 'item_name', 'amount', 'provider', 'status', 'created_at'],
                'fields'    => [
                    'reference'       => ['type' => 'text', 'readonly' => true],
                    'name'            => ['type' => 'text', 'required' => true],
                    'email'           => ['type' => 'text', 'required' => true],
                    'phone'           => ['type' => 'text'],
                    'item_type'       => ['type' => 'select', 'options' => ['treatment' => 'Treatment', 'package' => 'Package']],
                    'item_name'       => ['type' => 'text', 'required' => true, 'label' => 'Treatment / package'],
                    'preferred_date'  => ['type' => 'text', 'help' => 'The guest’s requested date and time'],
                    'notes'           => ['type' => 'textarea'],
                    'amount'          => ['type' => 'text', 'readonly' => true, 'label' => 'Deposit (USD)'],
                    'provider'        => ['type' => 'text', 'readonly' => true, 'label' => 'Paid via'],
                    'provider_ref'    => ['type' => 'text', 'readonly' => true, 'label' => 'Gateway reference'],
                    'status'          => ['type' => 'select', 'options' => ['pending' => 'Pending', 'paid' => 'Paid', 'failed' => 'Failed', 'cancelled' => 'Cancelled'], 'default' => 'pending'],
                    'gateway_message' => ['type' => 'text', 'readonly' => true],
                ],
            ],
            'faqs' => [
                'label'   => 'FAQs',
                'table'   => 'faqs',
                'icon'    => '❓',
                'order'   => 'sort_order, id',
                'columns' => ['question', 'active'],
                'fields'  => [
                    'question'   => ['type' => 'text', 'required' => true],
                    'answer'     => ['type' => 'textarea', 'required' => true],
                    'active'     => ['type' => 'checkbox', 'default' => 1],
                    'sort_order' => ['type' => 'number', 'default' => 0],
                ],
            ],
            'pages' => [
                'label'   => 'Pages',
                'table'   => 'pages',
                'icon'    => '📄',
                'columns' => ['slug', 'title', 'active'],
                'fields'  => [
                    'slug'             => ['type' => 'text', 'required' => true],
                    'title'            => ['type' => 'text', 'required' => true],
                    'body'             => ['type' => 'richtext', 'required' => true],
                    'meta_title'       => ['type' => 'text'],
                    'meta_description' => ['type' => 'textarea'],
                    'active'           => ['type' => 'checkbox', 'default' => 1],
                ],
            ],
            'homepage_sections' => [
                'label'   => 'Homepage',
                'table'   => 'homepage_sections',
                'icon'    => '🏠',
                'columns' => ['section', 'active'],
                'canCreate' => false,
                'canDelete' => false,
                'fields'  => [
                    'section' => ['type' => 'text', 'readonly' => true],
                    'content' => ['type' => 'json', 'required' => true, 'rows' => 16, 'help' => 'Structured content for this homepage section'],
                    'active'  => ['type' => 'checkbox', 'default' => 1],
                ],
            ],
            'users' => [
                'label'   => 'Users',
                'table'   => 'users',
                'icon'    => '🔐',
                'columns' => ['name', 'email', 'active'],
                'fields'  => [
                    'name'          => ['type' => 'text', 'required' => true],
                    'email'         => ['type' => 'text', 'required' => true],
                    'password_hash' => ['type' => 'password', 'label' => 'Password', 'help' => 'Leave blank to keep the current password'],
                    'role_id'       => ['type' => 'fk', 'table' => 'roles', 'label' => 'Role', 'default' => 1],
                    'active'        => ['type' => 'checkbox', 'default' => 1],
                ],
            ],
        ];
    }

    /** @return array<string, mixed>|null */
    public static function get(string $key): ?array
    {
        return self::all()[$key] ?? null;
    }
}
