<?php

namespace WooNooW\Templates;

defined('ABSPATH') || exit;

class TemplateRegistry
{
    /**
     * Get all available templates
     */
    public static function get_templates()
    {
        return apply_filters('woonoow_page_templates', [
            [
                'id' => 'blank',
                'label' => 'Blank Page',
                'description' => 'Start from scratch with an empty page.',
                'icon' => 'file',
                'sections' => []
            ],
            [
                'id' => 'landing-page',
                'label' => 'Landing Page',
                'description' => 'High-converting landing page with Hero, Features, and CTA.',
                'icon' => 'layout',
                'sections' => self::get_landing_page_structure()
            ],
            [
                'id' => 'about-us',
                'label' => 'About Us',
                'description' => 'Tell your story with image-text layouts and content.',
                'icon' => 'users',
                'sections' => self::get_about_us_structure()
            ],
            [
                'id' => 'contact',
                'label' => 'Contact',
                'description' => 'Simple contact page with a form and address details.',
                'icon' => 'mail',
                'sections' => self::get_contact_structure()
            ],
            [
                'id' => 'single-post',
                'label' => 'Single Post / CPT',
                'description' => 'A dynamic layout for blog posts or custom post types with a hero, featured image, and body content.',
                'icon' => 'layout',
                'sections' => self::get_single_post_structure()
            ]
        ]);
    }

    /**
     * Get a specific template by ID
     */
    public static function get_template($id)
    {
        $templates = self::get_templates();
        foreach ($templates as $template) {
            if ($template['id'] === $id) {
                return $template;
            }
        }
        return null;
    }

    /**
     * Helper to generate a unique ID
     */
    private static function generate_id()
    {
        return uniqid('section_');
    }

    private static function get_landing_page_structure()
    {
        return [
            [
                'id' => self::generate_id(),
                'type' => 'hero',
                'props' => [
                    'title' => ['type' => 'static', 'value' => 'Welcome to Our Service'],
                    'subtitle' => ['type' => 'static', 'value' => 'We create amazing digital experiences for your business.'],
                    'cta_text' => ['type' => 'static', 'value' => 'Get Started'],
                    'cta_url' => ['type' => 'static', 'value' => '#'],
                    'image' => ['type' => 'static', 'value' => ''],
                ],
                'styles' => ['contentWidth' => 'full']
            ],
            [
                'id' => self::generate_id(),
                'type' => 'feature-grid',
                'props' => [
                    'heading' => ['type' => 'static', 'value' => 'Why Choose Us'],
                    'features' => ['type' => 'static', 'value' => [
                        ['title' => 'Fast Delivery', 'description' => 'Quick shipping to your doorstep'],
                        ['title' => 'Secure Payment', 'description' => 'Your data is always protected'],
                        ['title' => 'Quality Products', 'description' => 'Only the best for our customers']
                    ]]
                ],
                'styles' => ['contentWidth' => 'contained']
            ],
            [
                'id' => self::generate_id(),
                'type' => 'cta-banner',
                'props' => [
                    'title' => ['type' => 'static', 'value' => 'Ready to Launch?'],
                    'text' => ['type' => 'static', 'value' => 'Join thousands of satisfied customers today.'],
                    'button_text' => ['type' => 'static', 'value' => 'Sign Up Now'],
                    'button_url' => ['type' => 'static', 'value' => '#']
                ],
                'styles' => ['contentWidth' => 'full']
            ]
        ];
    }

    private static function get_about_us_structure()
    {
        return [
            [
                'id' => self::generate_id(),
                'type' => 'image-text',
                'layoutVariant' => 'image-left',
                'props' => [
                    'title' => ['type' => 'static', 'value' => 'Our Story'],
                    'text' => ['type' => 'static', 'value' => 'We started with a simple mission: to make web design accessible to everyone. Our journey began in a small garage...'],
                    'image' => ['type' => 'static', 'value' => '']
                ],
                'styles' => ['contentWidth' => 'contained']
            ],
            [
                'id' => self::generate_id(),
                'type' => 'image-text',
                'layoutVariant' => 'image-right',
                'props' => [
                    'title' => ['type' => 'static', 'value' => 'Our Vision'],
                    'text' => ['type' => 'static', 'value' => 'To empower businesses of all sizes to have a professional online presence without the technical headache.'],
                    'image' => ['type' => 'static', 'value' => '']
                ],
                'styles' => ['contentWidth' => 'contained']
            ],
            [
                'id' => self::generate_id(),
                'type' => 'content',
                'props' => [
                    'content' => ['type' => 'static', 'value' => '<h3>Meet the Team</h3><p>Our diverse team of designers and developers...</p>']
                ],
                'styles' => ['contentWidth' => 'contained']
            ]
        ];
    }

    private static function get_contact_structure()
    {
        return [
            [
                'id' => self::generate_id(),
                'type' => 'content',
                'props' => [
                    'content' => ['type' => 'static', 'value' => '<h2>Get in Touch</h2><p>We are here to help and answer any question you might have.</p><p><strong>Address:</strong><br>123 Web Street<br>Tech City, TC 90210</p>']
                ],
                'styles' => ['contentWidth' => 'contained']
            ],
            [
                'id' => self::generate_id(),
                'type' => 'contact-form',
                'props' => [
                    'title' => ['type' => 'static', 'value' => 'Send us a Message'],
                    'webhook_url' => ['type' => 'static', 'value' => ''],
                    'redirect_url' => ['type' => 'static', 'value' => '']
                ],
                'styles' => ['contentWidth' => 'contained']
            ]
        ];
    }

    private static function get_single_post_structure()
    {
        return [
            // ── Section 1: Article Hero ─────────────────────────────────────
            [
                'id'           => self::generate_id(),
                'type'         => 'hero',
                'layoutVariant' => 'centered',
                'colorScheme'  => 'default',
                'props'        => [
                    'title'    => ['type' => 'dynamic', 'source' => 'post_title'],
                    'subtitle' => ['type' => 'dynamic', 'source' => 'post_author'],
                    'image'    => ['type' => 'static',  'value'  => ''],
                    'cta_text' => ['type' => 'static',  'value'  => ''],
                    'cta_url'  => ['type' => 'static',  'value'  => ''],
                ],
                // dynamicBackground tells the API to resolve styles.backgroundImage
                // from 'post_featured_image' at render time (falls back to '' if no featured image)
                'styles'       => [
                    'contentWidth'      => 'contained',
                    'heightPreset'      => 'medium',
                    'dynamicBackground' => 'post_featured_image',
                    'backgroundOverlay' => 50,
                ],
            ],

            // ── Section 2: Article Body ─────────────────────────────────────
            [
                'id'           => self::generate_id(),
                'type'         => 'content',
                'layoutVariant' => 'narrow',
                'colorScheme'  => 'default',
                'props'        => [
                    'content'  => ['type' => 'dynamic', 'source' => 'post_content'],
                    'cta_text' => ['type' => 'static',  'value'  => ''],
                    'cta_url'  => ['type' => 'static',  'value'  => ''],
                ],
                'styles'       => ['contentWidth' => 'contained', 'heightPreset' => 'default'],
            ],

            // ── Section 3: Related Posts ────────────────────────────────────
            [
                'id'           => self::generate_id(),
                'type'         => 'feature-grid',
                'layoutVariant' => 'grid-3',
                'colorScheme'  => 'muted',
                'props'        => [
                    'heading'  => ['type' => 'static',  'value'  => 'Related Articles'],
                    'features' => ['type' => 'dynamic', 'source' => 'related_posts'],
                ],
                'styles'       => ['contentWidth' => 'contained', 'heightPreset' => 'default'],
            ],

            // ── Section 4: CTA Banner ───────────────────────────────────────
            [
                'id'           => self::generate_id(),
                'type'         => 'cta-banner',
                'colorScheme'  => 'gradient',
                'props'        => [
                    'title'       => ['type' => 'static', 'value' => 'Enjoyed this article?'],
                    'text'        => ['type' => 'static', 'value' => 'Subscribe to our newsletter and never miss an update.'],
                    'button_text' => ['type' => 'static', 'value' => 'Subscribe Now'],
                    'button_url'  => ['type' => 'static', 'value' => '/subscribe'],
                ],
                'styles'       => ['contentWidth' => 'contained', 'heightPreset' => 'medium'],
            ],
        ];
    }
}
