<?php

declare(strict_types=1);

final class MyronFooterZoneContentSchemaService
{
    /**
     * @param array<string, mixed> $content
     * @return array{fields:list<array<string, mixed>>,repeaters:list<array<string, mixed>>}
     */
    public function schemaForZone(string $zoneId, array $content = []): array
    {
        $fields    = $this->fieldsForZone($zoneId, $content);
        $repeaters = $this->repeatersForZone($zoneId, $content);

        return ['fields' => $fields, 'repeaters' => $repeaters];
    }

    /**
     * @return list<string>
     */
    public function knownZoneIds(): array
    {
        return [
            'zone.brand',
            'zone.nav-col',
            'zone.cta-band',
            'zone.newsletter',
            'zone.social',
            'zone.legal-strip',
            'zone.trust-signals',
            'zone.nap-hours',
            'zone.disclaimer',
            'zone.payment-logos',
            'zone.app-badges',
            'zone.availability-status',
            'zone.provider-cta',
        ];
    }

    /**
     * @param array<string, mixed> $content
     * @return list<array<string, mixed>>
     */
    private function fieldsForZone(string $zoneId, array $content): array
    {
        return match ($zoneId) {
            'zone.brand' => [
                $this->field('logo_url', 'Logo', 'asset', $content),
                $this->field('logo_alt', 'Logo Alt Text', 'text', $content),
                $this->field('site_name', 'Site Name', 'text', $content),
                $this->field('tagline', 'Tagline', 'text', $content),
            ],
            'zone.nav-col' => [
                $this->field('heading', 'Column Heading', 'text', $content),
            ],
            'zone.cta-band' => [
                $this->field('headline', 'Headline', 'text', $content),
                $this->field('supporting_copy', 'Supporting Copy', 'textarea', $content),
                $this->field('primary_cta_label', 'Primary CTA Label', 'text', $content),
                $this->field('primary_cta_url', 'Primary CTA URL', 'text', $content),
                $this->field('secondary_cta_label', 'Secondary CTA Label', 'text', $content),
                $this->field('secondary_cta_url', 'Secondary CTA URL', 'text', $content),
                $this->field('trust_microcopy', 'Trust Micro-copy', 'text', $content),
            ],
            'zone.newsletter' => [
                $this->field('heading', 'Heading', 'text', $content),
                $this->field('supporting_copy', 'Supporting Copy', 'text', $content),
                $this->field('email_placeholder', 'Email Placeholder', 'text', $content),
                $this->field('submit_label', 'Submit Button Label', 'text', $content),
                $this->field('consent_note', 'Consent Note', 'textarea', $content),
            ],
            'zone.legal-strip' => [
                $this->field('copyright_text', 'Copyright Text', 'text', $content),
            ],
            'zone.disclaimer' => [
                $this->field('disclaimer_text', 'Disclaimer Text', 'textarea', $content),
            ],
            'zone.availability-status' => [
                $this->field('status_text', 'Status Text', 'text', $content),
                $this->field('contact_url', 'Contact URL', 'text', $content),
            ],
            'zone.nap-hours' => [
                $this->field('business_name', 'Business Name', 'text', $content),
                $this->field('street_address', 'Street Address', 'text', $content),
                $this->field('city_state_zip', 'City, State, ZIP', 'text', $content),
                $this->field('phone', 'Phone', 'text', $content),
                $this->field('email', 'Email', 'text', $content),
                $this->field('maps_url', 'Google Maps URL', 'text', $content),
            ],
            default => [],
        };
    }

    /**
     * @param array<string, mixed> $content
     * @return list<array<string, mixed>>
     */
    private function repeatersForZone(string $zoneId, array $content): array
    {
        return match ($zoneId) {
            'zone.nav-col', 'zone.provider-cta' => [
                $this->repeater('links', 'Links', 'Link', [
                    $this->repeaterField('label', 'Label'),
                    $this->repeaterField('url', 'URL'),
                ], $content, [
                    ['label' => '', 'url' => ''],
                    ['label' => '', 'url' => ''],
                ]),
            ],
            'zone.social' => [
                $this->repeater('platforms', 'Platforms', 'Platform', [
                    $this->repeaterField('platform_id', 'Platform'),
                    $this->repeaterField('icon_url', 'Icon', 'asset'),
                    $this->repeaterField('url', 'URL'),
                    $this->repeaterField('label', 'Label'),
                ], $content, [
                    ['platform_id' => '', 'icon_url' => '', 'url' => '', 'label' => ''],
                ]),
            ],
            'zone.legal-strip' => [
                $this->repeater('links', 'Legal Links', 'Link', [
                    $this->repeaterField('label', 'Label'),
                    $this->repeaterField('url', 'URL'),
                ], $content, [
                    ['label' => 'Privacy Policy', 'url' => '/legal/privacy'],
                    ['label' => 'Terms', 'url' => '/legal/terms'],
                ]),
            ],
            'zone.trust-signals' => [
                $this->repeater('badges', 'Badges', 'Badge', [
                    $this->repeaterField('image_url', 'Image', 'asset'),
                    $this->repeaterField('alt', 'Alt Text'),
                    $this->repeaterField('link_url', 'Link URL'),
                ], $content, [
                    ['image_url' => '', 'alt' => '', 'link_url' => ''],
                ]),
            ],
            'zone.nap-hours' => [
                $this->repeater('hours', 'Hours', 'Day', [
                    $this->repeaterField('day', 'Day'),
                    $this->repeaterField('open_time', 'Opens'),
                    $this->repeaterField('close_time', 'Closes'),
                ], $content, [
                    ['day' => 'Monday–Friday', 'open_time' => '', 'close_time' => ''],
                    ['day' => 'Saturday', 'open_time' => '', 'close_time' => ''],
                    ['day' => 'Sunday', 'open_time' => 'Closed', 'close_time' => ''],
                ]),
            ],
            'zone.payment-logos' => [
                $this->repeater('methods', 'Payment Methods', 'Method', [
                    $this->repeaterField('method_id', 'Method'),
                    $this->repeaterField('image_url', 'Logo', 'asset'),
                    $this->repeaterField('alt', 'Alt Text'),
                ], $content, [
                    ['method_id' => '', 'image_url' => '', 'alt' => ''],
                ]),
            ],
            'zone.app-badges' => [
                $this->repeater('badges', 'App Store Badges', 'Badge', [
                    $this->repeaterField('store_id', 'Store'),
                    $this->repeaterField('image_url', 'Badge', 'asset'),
                    $this->repeaterField('url', 'Store URL'),
                    $this->repeaterField('alt', 'Alt Text'),
                ], $content, [
                    ['store_id' => 'ios', 'image_url' => '', 'url' => '', 'alt' => 'Download on the App Store'],
                    ['store_id' => 'android', 'image_url' => '', 'url' => '', 'alt' => 'Get it on Google Play'],
                ]),
            ],
            default => [],
        };
    }

    /**
     * @param array<string, mixed> $content
     * @return array<string, mixed>
     */
    private function field(string $key, string $label, string $type, array $content): array
    {
        return [
            'key'   => $key,
            'label' => $label,
            'type'  => $type,
            'value' => trim((string) ($content[$key] ?? '')),
        ];
    }

    /**
     * @param list<array<string, mixed>> $fields
     * @param array<string, mixed> $content
     * @param list<array<string, mixed>> $fallbackItems
     * @return array<string, mixed>
     */
    private function repeater(
        string $key,
        string $label,
        string $itemLabel,
        array $fields,
        array $content,
        array $fallbackItems
    ): array {
        $items = is_array($content[$key] ?? null)
            ? array_values(array_filter($content[$key], 'is_array'))
            : [];

        if ($items === []) {
            $items = $fallbackItems;
        }

        return [
            'key'       => $key,
            'label'     => $label,
            'itemLabel' => $itemLabel,
            'type'      => 'repeater',
            'fields'    => $fields,
            'items'     => $items,
        ];
    }

    /**
     * @return array<string, mixed>
     */
    private function repeaterField(string $key, string $label, string $type = 'text'): array
    {
        return ['key' => $key, 'label' => $label, 'type' => $type];
    }
}
