<?php

namespace App;

use App\Enums\SpecDocumentSectionType;

/**
 * Field schemas for the predefined spec document sections. The admin UI renders each
 * section generically from this blueprint, so section content stays a free-form JSON
 * payload on the section record rather than dedicated columns.
 *
 * Supported field types: text, textarea, checkbox, radio, select, multiselect, repeater.
 * Selects may set `taggable` to allow values outside `options`, and `options_source`
 * to resolve options from the options endpoint payload (e.g. the integration list).
 */
class SpecDocumentBlueprint
{
    public function schemas(): array
    {
        return collect(SpecDocumentSectionType::cases())
            ->map(fn (SpecDocumentSectionType $type) => [
                'type' => $type->value,
                'label' => $type->label(),
                'fields' => $this->fields($type),
            ])
            ->values()
            ->all();
    }

    /**
     * Map of section field keys that are backed by a tenant column, keyed by field key
     * with the tenant column as the value. These are stored on the tenant, not in the
     * section's JSON data.
     *
     * @return array<string, string>
     */
    public function tenantFields(string $type): array
    {
        $sectionType = SpecDocumentSectionType::tryFrom($type);
        if (!$sectionType) {
            return [];
        }

        return collect($this->fields($sectionType))
            ->filter(fn (array $field) => !empty($field['tenant_field']))
            ->mapWithKeys(fn (array $field) => [$field['key'] => $field['tenant_field']])
            ->all();
    }

    /**
     * Section types whose content is driven by a central template: new documents auto-fill
     * these from the current template, and each instance can then be adjusted.
     *
     * @return array<SpecDocumentSectionType>
     */
    public function templatableTypes(): array
    {
        return [
            SpecDocumentSectionType::INTRODUCTION,
            SpecDocumentSectionType::ADDITIONAL_INFORMATION,
            SpecDocumentSectionType::ASSUMPTIONS,
            SpecDocumentSectionType::SYSTEM_FEATURES,
            SpecDocumentSectionType::CHANGE_CONTROL,
        ];
    }

    public function isTemplatable(string $type): bool
    {
        return in_array(SpecDocumentSectionType::tryFrom($type), $this->templatableTypes(), true);
    }

    public function fields(SpecDocumentSectionType $type): array
    {
        return match ($type) {
            SpecDocumentSectionType::INTRODUCTION => [
                ['key' => 'overview', 'label' => 'Project Overview', 'type' => 'textarea'],
                [
                    'key' => 'objectives',
                    'label' => 'Objectives',
                    'type' => 'repeater',
                    'item_label' => 'Objective',
                    'fields' => [
                        ['key' => 'objective', 'label' => 'Objective', 'type' => 'text'],
                    ],
                ],
                [
                    'key' => 'target_launch_date',
                    'label' => 'Target Launch Date',
                    'type' => 'date',
                    'tenant_field' => 'proposed_launch_date',
                ],
                [
                    'key' => 'pre_launch_monthly_enquiries',
                    'label' => 'Pre-Launch Monthly Enquiries',
                    'type' => 'number',
                    'tenant_field' => 'pre_launch_monthly_enquiries',
                ],
                [
                    'key' => 'pre_launch_daily_enquiries',
                    'label' => 'Pre-Launch Daily Enquiries',
                    'type' => 'number',
                    'tenant_field' => 'pre_launch_daily_enquiries',
                ],
                [
                    'key' => 'pre_launch_daily_visitors',
                    'label' => 'Pre-Launch Daily Visitors',
                    'type' => 'number',
                    'tenant_field' => 'pre_launch_daily_visitors',
                ],
                [
                    'key' => 'current_statistics',
                    'label' => 'Other Website Statistics',
                    'type' => 'repeater',
                    'item_label' => 'Statistic',
                    'display' => 'table',
                    'fields' => [
                        ['key' => 'label', 'label' => 'Label', 'type' => 'text'],
                        ['key' => 'value', 'label' => 'Value', 'type' => 'text'],
                    ],
                ],
            ],
            SpecDocumentSectionType::COMPANY_INFORMATION => [
                ['key' => 'company_name', 'label' => 'Company Name', 'type' => 'text'],
                ['key' => 'registered_address', 'label' => 'Registered Address', 'type' => 'text'],
                ['key' => 'contact_name', 'label' => 'Main Contact Name', 'type' => 'text'],
                ['key' => 'contact_email', 'label' => 'Main Contact Email', 'type' => 'text'],
                ['key' => 'contact_phone', 'label' => 'Main Contact Phone', 'type' => 'text'],
                ['key' => 'contact_role', 'label' => 'Main Contact Role', 'type' => 'text'],
                ['key' => 'region', 'label' => 'Region', 'type' => 'select', 'taggable' => true, 'options' => [
                    'England', 'Scotland', 'Wales', 'Republic of Ireland', 'Northern Ireland',
                ]],
                ['key' => 'odometer_units', 'label' => 'Odometer Units', 'type' => 'radio', 'options' => [
                    'Miles', 'Kilometres',
                ]],
                ['key' => 'currency_units', 'label' => 'Currency Units', 'type' => 'select', 'taggable' => true, 'options' => [
                    'GBP', 'Euros', 'Dollars',
                ]],
                ['key' => 'project_type', 'label' => 'Project Type', 'type' => 'radio', 'options' => [
                    'New - Independent, Single Location',
                    'New - Independent, Multi-Location',
                    'New - Single Franchise, Single Location',
                    'New - Multi-Franchise, Single Location',
                    'New - Single Franchise, Multi-Location',
                    'New - Multi-Franchise, Multi-Location',
                    'OEM',
                ]],
            ],
            SpecDocumentSectionType::LOCATIONS => [
                [
                    'key' => 'franchises',
                    'label' => 'Franchises',
                    'type' => 'repeater',
                    'item_label' => 'Franchise',
                    'fields' => [
                        ['key' => 'name', 'label' => 'Franchise', 'type' => 'text'],
                    ],
                ],
                [
                    'key' => 'locations',
                    'label' => 'Locations',
                    'type' => 'repeater',
                    'item_label' => 'Location',
                    'collapsible' => true,
                    'summary_fields' => ['franchise', 'name'],
                    'fields' => [
                        ['key' => 'name', 'label' => 'Location Name', 'type' => 'text'],
                        ['key' => 'address', 'label' => 'Address', 'type' => 'text'],
                        [
                            'key' => 'franchise',
                            'label' => 'Franchise',
                            'type' => 'select',
                            'options_source' => 'franchise_names',
                        ],
                        [
                            'key' => 'departments',
                            'label' => 'Departments',
                            'type' => 'repeater',
                            'item_label' => 'Department',
                            'display' => 'table',
                            'fields' => [
                                ['key' => 'name', 'label' => 'Department', 'type' => 'text'],
                                ['key' => 'weekday_hours', 'label' => 'Mon-Fri', 'type' => 'text'],
                                ['key' => 'saturday_hours', 'label' => 'Saturday', 'type' => 'text'],
                                ['key' => 'sunday_hours', 'label' => 'Sunday', 'type' => 'text'],
                                ['key' => 'phone', 'label' => 'Phone', 'type' => 'text'],
                                ['key' => 'email', 'label' => 'Email', 'type' => 'text'],
                            ],
                        ],
                    ],
                ],
            ],
            SpecDocumentSectionType::DESIGN => [
                ['key' => 'brand_guidelines', 'label' => 'Brand Guidelines', 'type' => 'textarea'],
                ['key' => 'design_brief', 'label' => 'Design Brief', 'type' => 'textarea'],
                [
                    'key' => 'initial_design_pages',
                    'label' => 'Pages For Initial Design',
                    'type' => 'repeater',
                    'item_label' => 'Page',
                    'fields' => [
                        ['key' => 'page', 'label' => 'Page', 'type' => 'text'],
                    ],
                ],
                ['key' => 'brand_font', 'label' => 'Brand Font', 'type' => 'text'],
                ['key' => 'brand_colours', 'label' => 'Brand Colours', 'type' => 'text'],
                ['key' => 'vehicle_search_filters', 'label' => 'Vehicle Search Filters', 'type' => 'multiselect', 'options' => [
                    'Condition (All, New, Used)', 'Make', 'Model', 'Price', 'Monthly Price', 'Mileage',
                    'Age', 'Transmission', 'Fuel Type', 'Body Type', 'Colour', 'Location',
                    'Engine Size', 'Number of Seats', 'Number of Doors',
                ]],
                [
                    'key' => 'media_links',
                    'label' => 'Social Media Links',
                    'type' => 'repeater',
                    'item_label' => 'Link',
                    'display' => 'table',
                    'fields' => [
                        ['key' => 'platform', 'label' => 'Platform', 'type' => 'select', 'taggable' => true, 'options' => [
                            'Facebook', 'X', 'Instagram', 'Pinterest', 'LinkedIn', 'YouTube', 'TikTok',
                        ]],
                        ['key' => 'url', 'label' => 'URL', 'type' => 'text'],
                    ],
                ],
            ],
            SpecDocumentSectionType::FEEDS => [
                [
                    'key' => 'inbound_feeds',
                    'label' => 'Inbound Feeds',
                    'type' => 'repeater',
                    'item_label' => 'Inbound Feed',
                    'display' => 'table',
                    'fields' => [
                        ['key' => 'required', 'label' => 'Required', 'type' => 'checkbox'],
                        ['key' => 'provider', 'label' => 'Provider', 'type' => 'select', 'taggable' => true, 'options_source' => 'stock_providers'],
                        ['key' => 'requirements', 'label' => 'Requirements', 'type' => 'text', 'multiline' => true],
                        ['key' => 'feed_id', 'label' => 'Feed ID', 'type' => 'text'],
                        ['key' => 'images', 'label' => 'Images Source', 'type' => 'text'],
                        ['key' => 'notes', 'label' => 'Notes', 'type' => 'text', 'multiline' => true],
                    ],
                ],
                [
                    'key' => 'outbound_feeds',
                    'label' => 'Outbound Feeds',
                    'type' => 'repeater',
                    'item_label' => 'Outbound Feed',
                    'display' => 'table',
                    'fields' => [
                        ['key' => 'required', 'label' => 'Required', 'type' => 'checkbox'],
                        ['key' => 'provider', 'label' => 'Provider', 'type' => 'select', 'taggable' => true, 'options_source' => 'sales_channel_providers'],
                        ['key' => 'requirements', 'label' => 'Requirements', 'type' => 'text', 'multiline' => true],
                        ['key' => 'feed_id', 'label' => 'Feed ID', 'type' => 'text'],
                        ['key' => 'images', 'label' => 'Images Source', 'type' => 'text'],
                        ['key' => 'notes', 'label' => 'Notes', 'type' => 'text', 'multiline' => true],
                    ],
                ],
            ],
            SpecDocumentSectionType::INTEGRATIONS => [
                [
                    'key' => 'integrations',
                    'label' => 'Integrations',
                    'type' => 'repeater',
                    'item_label' => 'Integration',
                    'display' => 'table',
                    'preview_key' => 'provider',
                    'fields' => [
                        ['key' => 'required', 'label' => 'Required', 'type' => 'checkbox'],
                        ['key' => 'type', 'label' => 'Type', 'type' => 'select', 'taggable' => true, 'options_source' => 'integration_types'],
                        [
                            'key' => 'provider',
                            'label' => 'Provider',
                            'type' => 'select',
                            'taggable' => true,
                            'options_source' => 'integration_providers_by_type',
                            'depends_on' => 'type',
                        ],
                        ['key' => 'notes', 'label' => 'Notes', 'type' => 'text', 'multiline' => true],
                    ],
                ],
            ],
            SpecDocumentSectionType::CONTENT => [
                ['key' => 'content_responsibility', 'label' => 'Content Responsibility', 'type' => 'textarea'],
                ['key' => 'migration_notes', 'label' => 'Migration Notes', 'type' => 'textarea'],
            ],
            SpecDocumentSectionType::USERS => [
                [
                    'key' => 'users',
                    'label' => 'Users',
                    'type' => 'repeater',
                    'item_label' => 'User',
                    'display' => 'table',
                    'fields' => [
                        ['key' => 'name', 'label' => 'Name', 'type' => 'text'],
                        ['key' => 'email', 'label' => 'Email Address', 'type' => 'text'],
                        ['key' => 'role', 'label' => 'Role', 'type' => 'select', 'options' => [
                            'Administrator', 'Manager', 'Editor', 'Writer', 'Customer Support', 'Marketing',
                        ]],
                    ],
                ],
            ],
            SpecDocumentSectionType::ENQUIRY_FORMS => [
                ['key' => 'notification_email', 'label' => 'Notification Email Address', 'type' => 'text'],
                ['key' => 'notes', 'label' => 'Notes', 'type' => 'textarea'],
            ],
            SpecDocumentSectionType::NOTES => [
                ['key' => 'notes', 'label' => 'Notes & Custom Requirements', 'type' => 'textarea'],
            ],
            SpecDocumentSectionType::DOMAINS => [
                ['key' => 'primary_domain', 'label' => 'Primary Domain', 'type' => 'text'],
                [
                    'key' => 'secondary_domains',
                    'label' => 'Secondary Domains',
                    'type' => 'repeater',
                    'item_label' => 'Domain',
                    'fields' => [
                        ['key' => 'domain', 'label' => 'Domain', 'type' => 'text'],
                    ],
                ],
                ['key' => 'domain_migration', 'label' => 'Domain Migration', 'type' => 'textarea'],
            ],
            SpecDocumentSectionType::ADDITIONAL_INFORMATION => [
                ['key' => 'content', 'label' => 'Content', 'type' => 'textarea', 'full' => true],
            ],
            SpecDocumentSectionType::ASSUMPTIONS => [
                ['key' => 'assumptions', 'label' => 'Assumptions and Dependencies', 'type' => 'textarea'],
            ],
            SpecDocumentSectionType::SYSTEM_FEATURES => [
                ['key' => 'content', 'label' => 'Content', 'type' => 'textarea', 'full' => true],
            ],
            SpecDocumentSectionType::CHANGE_CONTROL => [
                ['key' => 'change_control', 'label' => 'Change Control Process', 'type' => 'textarea'],
            ],
            SpecDocumentSectionType::CUSTOM => [
                ['key' => 'content', 'label' => 'Content', 'type' => 'textarea'],
            ],
        };
    }
}
