<?php

namespace FleekDash\Services\Dashboard;

/**
 * Dashboard Template Definitions
 *
 * Contains all default template configurations
 *
 * @since 2.2.0 Extracted from DashboardTemplateService for better organization
 */
class TemplateDefinitions
{
    /**
     * Get all default template definitions
     *
     * @return array Array of template definitions
     */
    public static function getDefaultTemplates()
    {
        return [
            self::getFleekDashDefault(),
            self::getEcommerceOverview(),
            self::getMarketingAnalytics(),
            self::getContentPerformance(),
            self::getBusinessOverview(),
        ];
    }

    /**
     * FleekDash Default Template
     */
    private static function getFleekDashDefault()
    {
        return [
            'name' => 'FleekDash Default',
            'description' => 'Default FleekDash dashboard with KPI cards, growth chart, and latest posts grid',
            'category' => 'default',
            'is_default' => 1,
            'is_active' => 1,
            'config' => json_encode([
                'layout' => [
                    'type' => 'grid',
                    'columns' => 12,
                    'rows' => 'auto',
                    'gap' => 6,
                    'containerClass' => 'flex flex-col gap-6 @container'
                ],
                'widgets' => [
                    // Top Row - Stats Cards and Main Chart
                    [
                        'id' => 'stats-cards-container',
                        'type' => 'container',
                        'className' => 'grid gap-6 @5xl:grid-cols-5 @2xl:grid-cols-1 grid-cols-1',
                        'children' => [
                            // Left Side - Stats Cards (2x2 Grid)
                            [
                                'id' => 'stats-cards-grid',
                                'type' => 'container',
                                'className' => '@4xl:col-span-2 grid gap-4 @lg:grid-cols-2 grid-cols-1',
                                'children' => [
                                    // Card 1: Published Content
                                    [
                                        'id' => 'published-content-card',
                                        'type' => 'chart-widget',
                                        'title' => 'Published Content',
                                        'description' => 'Total published content',
                                        'iconName' => 'chart',
                                        'cardSize' => 'default',
                                        'chartType' => 'line',
                                        'compact' => true,
                                        'showCardHeader' => false,
                                        'showGuides' => false,
                                        'showDots' => false,
                                        'strokeWidth' => 2,
                                        'fillColor' => 'var(--color-card-foreground)',
                                        'showYAxis' => false,
                                        'showXAxis' => false,
                                        'showLegend' => false,
                                        'showTimeRangeSelector' => false,
                                        'showHexagonBadge' => false,
                                        'headerSeparator' => false,
                                        'className' => 'h-full flex-1',
                                        'kpiInfo' => [
                                            'value' => '12.5%',
                                            'percentage' => '12.5',
                                            'badgeAttached' => true,
                                            'variationType' => 'positive',
                                            'subtitle' => 'Published Content',
                                            'showSubtitle' => true,
                                            'showVariation' => true
                                        ],
                                        'data' => [
                                            ['date' => '2024-01-01', 'value' => 69700],
                                            ['date' => '2024-01-02', 'value' => 70200],
                                            ['date' => '2024-01-03', 'value' => 69800],
                                            ['date' => '2024-01-04', 'value' => 70500],
                                            ['date' => '2024-01-05', 'value' => 71000]
                                        ]
                                    ],
                                    // Card 2: Users Engagement
                                    [
                                        'id' => 'users-engagement-card',
                                        'type' => 'chart-widget',
                                        'title' => 'Users Engagement',
                                        'description' => 'Total users engagement',
                                        'iconName' => 'chart',
                                        'cardSize' => 'default',
                                        'chartType' => 'line',
                                        'compact' => true,
                                        'showCardHeader' => false,
                                        'showGuides' => false,
                                        'showDots' => false,
                                        'strokeWidth' => 2,
                                        'fillColor' => 'var(--color-card-foreground)',
                                        'showYAxis' => false,
                                        'showXAxis' => false,
                                        'showLegend' => false,
                                        'showTimeRangeSelector' => false,
                                        'showHexagonBadge' => false,
                                        'headerSeparator' => false,
                                        'className' => 'h-full flex-1',
                                        'fill' => false,
                                        'kpiInfo' => [
                                            'value' => '12.5%',
                                            'percentage' => '12.5',
                                            'badgeAttached' => true,
                                            'variationType' => 'positive',
                                            'subtitle' => 'Users Engagement',
                                            'showSubtitle' => true,
                                            'showVariation' => true
                                        ],
                                        'data' => [
                                            ['date' => '2024-01-01', 'value' => 29420],
                                            ['date' => '2024-01-02', 'value' => 29800],
                                            ['date' => '2024-01-03', 'value' => 29500],
                                            ['date' => '2024-01-04', 'value' => 30100],
                                            ['date' => '2024-01-05', 'value' => 30500]
                                        ]
                                    ],
                                    // Card 3: Growth Rate
                                    [
                                        'id' => 'growth-rate-card',
                                        'type' => 'stats-card-with-progress',
                                        'title' => 'Growth Rate',
                                        'value' => '1,836',
                                        'description' => 'Growth Rate',
                                        'goalValue' => '3,000',
                                        'currentProgress' => 61,
                                        'goalRemaining' => '1,164 to Goal',
                                        'changeValue' => '2.2%',
                                        'changeType' => 'negative',
                                        'cardSize' => 'default',
                                        'progressColor' => 'bg-green-500',
                                        'className' => 'h-full flex-1'
                                    ],
                                    // Card 4: New Users
                                    [
                                        'id' => 'new-users-card',
                                        'type' => 'stats-card-with-avatars',
                                        'value' => '6.3k',
                                        'description' => 'New Users',
                                        'changeValue' => '8.3%',
                                        'changeType' => 'positive',
                                        'avatarGroupTitle' => "Today's Heroes",
                                        'avatars' => [
                                            ['fallback' => 'S', 'className' => 'bg-green-500 text-white'],
                                            ['src' => 'https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=200&h=200&fit=crop&crop=face'],
                                            ['fallback' => 'A', 'className' => 'bg-blue-500 text-white'],
                                            ['src' => 'https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=200&h=200&fit=crop&crop=face'],
                                            ['fallback' => '+42', 'className' => 'bg-purple-500 text-white']
                                        ],
                                        'maxVisible' => 6,
                                        'avatarSize' => 'xl',
                                        'cardSize' => 'default',
                                        'badgeAttached' => true,
                                        'className' => 'h-full flex-1'
                                    ]
                                ]
                            ],
                            // Right Side - Main Chart
                            [
                                'id' => 'growth-evolution-chart',
                                'type' => 'chart-widget',
                                'title' => 'Growth Evolution',
                                'description' => '+20.1% from last period',
                                'chartType' => 'area',
                                'yKeys' => ['current', 'previous'],
                                'height' => 300,
                                'showGuides' => true,
                                'showLegend' => false,
                                'showTooltip' => true,
                                'showXAxis' => true,
                                'showYAxis' => true,
                                'showTimeRangeSelector' => true,
                                'exportable' => true,
                                'loading' => false,
                                'compact' => false,
                                'smoothCurves' => false,
                                'showDots' => false,
                                'dotColors' => ['#009DFF', '#ff6b35'],
                                'horizontalGradient' => false,
                                'glowEffect' => true,
                                'glowColor' => '#f1c9ff',
                                'glowIntensity' => 'none',
                                'glowPulse' => false,
                                'simulateTimeRangeData' => true,
                                'usePrimaryColors' => false,
                                'cardSize' => 'default',
                                'cardVariant' => 'default',
                                'cardInteractive' => 'none',
                                'cardStatus' => 'default',
                                'disabled' => false,
                                'collapsible' => false,
                                'forceDark' => true,
                                'customGradient' => false,
                                'customGradientColor1' => '#45057c',
                                'customGradientColor2' => '#3f144c',
                                'gradientBackground' => 'grad-cosmic-01',
                                'strokeWidth' => 2,
                                'fillOpacity' => 0.5,
                                'fillColor' => '#ffffff',
                                'headerSeparator' => false,
                                'useDateRangeCalendar' => true,
                                'className' => '@4xl:col-span-3',
                                'defaultDateRange' => [
                                    'from' => '30d-ago',
                                    'to' => 'today'
                                ]
                            ]
                        ]
                    ],
                    // Bottom Row - Latest Posts Grid
                    [
                        'id' => 'latest-posts-card',
                        'type' => 'card',
                        'className' => '',
                        'header' => [
                            'title' => 'Latest Posts',
                            'description' => 'Quick approve or reject posts here.',
                            'actions' => [
                                [
                                    'type' => 'button',
                                    'variant' => 'secondary',
                                    'size' => 'sm',
                                    'successText' => 'Approved',
                                    'icon' => 'check',
                                    'text' => 'Approve All'
                                ],
                                [
                                    'type' => 'button',
                                    'variant' => 'secondary',
                                    'size' => 'sm',
                                    'successText' => 'Rejected',
                                    'icon' => 'close',
                                    'text' => 'Reject All'
                                ]
                            ]
                        ],
                        'content' => [
                            'type' => 'grid-posts',
                            'className' => 'h-[400px]',
                            'config' => [
                                'postType' => 'all',
                                'postTypeLabel' => 'Latest Posts',
                                'enableVirtualization' => false,
                                'emptyStateTitle' => 'No posts found',
                                'emptyStateDescription' => 'There are no recent posts to display.',
                                'customColumns' => [['id' => 'category', 'visible' => false]],
                                'hideHeader' => true,
                                'enableResponsiveColumns' => true,
                                'limit' => 5,
                                'orderby' => 'date',
                                'order' => 'desc'
                            ]
                        ]
                    ]
                ],
                'filters' => [
                    'timeRange' => '30d',
                    'sources' => ['wordpress'],
                    'categories' => ['content', 'users', 'analytics']
                ],
                'metadata' => [
                    'version' => '1.0',
                    'created' => '2024-01-01',
                    'author' => 'FleekDash',
                    'description' => 'Default FleekDash dashboard layout with KPI cards, growth chart, and latest posts grid'
                ]
            ])
        ];
    }

    /**
     * E-commerce Overview Template
     */
    private static function getEcommerceOverview()
    {
        return [
            'name' => 'E-commerce Overview',
            'description' => 'Complete e-commerce dashboard with sales, orders, and customer metrics',
            'category' => 'ecommerce',
            'is_default' => 0,
            'is_active' => 1,
            'config' => json_encode([
                'widgets' => [
                    [
                        'id' => 'revenue-overview',
                        'type' => 'chart',
                        'title' => 'Revenue Overview',
                        'metric' => 'revenue',
                        'category' => 'sales',
                        'chartType' => 'area',
                        'timeRange' => '30d',
                        'position' => ['x' => 0, 'y' => 0, 'w' => 6, 'h' => 4]
                    ],
                    [
                        'id' => 'orders-summary',
                        'type' => 'chart',
                        'title' => 'Orders Summary',
                        'metric' => 'orders',
                        'category' => 'sales',
                        'chartType' => 'bar',
                        'timeRange' => '30d',
                        'position' => ['x' => 6, 'y' => 0, 'w' => 6, 'h' => 4]
                    ],
                    [
                        'id' => 'top-products',
                        'type' => 'chart',
                        'title' => 'Top Products',
                        'metric' => 'product_performance',
                        'category' => 'sales',
                        'chartType' => 'bar',
                        'timeRange' => '30d',
                        'position' => ['x' => 0, 'y' => 4, 'w' => 6, 'h' => 4]
                    ],
                    [
                        'id' => 'customer-metrics',
                        'type' => 'chart',
                        'title' => 'Customer Metrics',
                        'metric' => 'customer_engagement',
                        'category' => 'users',
                        'chartType' => 'line',
                        'timeRange' => '30d',
                        'position' => ['x' => 6, 'y' => 4, 'w' => 6, 'h' => 4]
                    ]
                ],
                'filters' => [
                    'timeRange' => '30d',
                    'sources' => ['woocommerce', 'wordpress'],
                    'categories' => ['sales', 'users']
                ]
            ])
        ];
    }

    /**
     * Marketing Analytics Template
     */
    private static function getMarketingAnalytics()
    {
        return [
            'name' => 'Marketing Analytics',
            'description' => 'Marketing-focused dashboard with traffic, conversions, and campaign metrics',
            'category' => 'marketing',
            'is_default' => 0,
            'is_active' => 1,
            'config' => json_encode([
                'widgets' => [
                    [
                        'id' => 'traffic-overview',
                        'type' => 'chart',
                        'title' => 'Traffic Overview',
                        'metric' => 'unique_visitors',
                        'category' => 'traffic',
                        'chartType' => 'area',
                        'timeRange' => '30d',
                        'position' => ['x' => 0, 'y' => 0, 'w' => 6, 'h' => 4]
                    ],
                    [
                        'id' => 'conversion-rate',
                        'type' => 'chart',
                        'title' => 'Conversion Rate',
                        'metric' => 'conversion_rate',
                        'category' => 'sales',
                        'chartType' => 'line',
                        'timeRange' => '30d',
                        'position' => ['x' => 6, 'y' => 0, 'w' => 6, 'h' => 4]
                    ],
                    [
                        'id' => 'traffic-sources',
                        'type' => 'chart',
                        'title' => 'Traffic Sources',
                        'metric' => 'referrers',
                        'category' => 'traffic',
                        'chartType' => 'bar',
                        'timeRange' => '30d',
                        'position' => ['x' => 0, 'y' => 4, 'w' => 6, 'h' => 4]
                    ],
                    [
                        'id' => 'campaign-performance',
                        'type' => 'chart',
                        'title' => 'Campaign Performance',
                        'metric' => 'campaign_metrics',
                        'category' => 'marketing',
                        'chartType' => 'bar',
                        'timeRange' => '30d',
                        'position' => ['x' => 6, 'y' => 4, 'w' => 6, 'h' => 4]
                    ]
                ],
                'filters' => [
                    'timeRange' => '30d',
                    'sources' => ['google_analytics', 'wordpress', 'facebook'],
                    'categories' => ['traffic', 'sales', 'marketing']
                ]
            ])
        ];
    }

    /**
     * Content Performance Template
     */
    private static function getContentPerformance()
    {
        return [
            'name' => 'Content Performance',
            'description' => 'Content-focused dashboard with post performance and engagement metrics',
            'category' => 'content',
            'is_default' => 0,
            'is_active' => 1,
            'config' => json_encode([
                'widgets' => [
                    [
                        'id' => 'popular-posts',
                        'type' => 'chart',
                        'title' => 'Popular Posts',
                        'metric' => 'popular_posts',
                        'category' => 'content',
                        'chartType' => 'bar',
                        'timeRange' => '30d',
                        'position' => ['x' => 0, 'y' => 0, 'w' => 6, 'h' => 4]
                    ],
                    [
                        'id' => 'content-engagement',
                        'type' => 'chart',
                        'title' => 'Content Engagement',
                        'metric' => 'content_engagement',
                        'category' => 'content',
                        'chartType' => 'line',
                        'timeRange' => '30d',
                        'position' => ['x' => 6, 'y' => 0, 'w' => 6, 'h' => 4]
                    ],
                    [
                        'id' => 'user-comments',
                        'type' => 'chart',
                        'title' => 'User Comments',
                        'metric' => 'comments',
                        'category' => 'content',
                        'chartType' => 'area',
                        'timeRange' => '30d',
                        'position' => ['x' => 0, 'y' => 4, 'w' => 6, 'h' => 4]
                    ],
                    [
                        'id' => 'social-shares',
                        'type' => 'chart',
                        'title' => 'Social Shares',
                        'metric' => 'social_shares',
                        'category' => 'content',
                        'chartType' => 'bar',
                        'timeRange' => '30d',
                        'position' => ['x' => 6, 'y' => 4, 'w' => 6, 'h' => 4]
                    ]
                ],
                'filters' => [
                    'timeRange' => '30d',
                    'sources' => ['wordpress', 'social_media'],
                    'categories' => ['content']
                ]
            ])
        ];
    }

    /**
     * Business Overview Template
     */
    private static function getBusinessOverview()
    {
        return [
            'name' => 'Business Overview',
            'description' => 'General business dashboard with key performance indicators',
            'category' => 'business',
            'is_default' => 1,
            'is_active' => 1,
            'config' => json_encode([
                'widgets' => [
                    [
                        'id' => 'revenue-trends',
                        'type' => 'chart',
                        'title' => 'Revenue Trends',
                        'metric' => 'revenue',
                        'category' => 'sales',
                        'chartType' => 'area',
                        'timeRange' => '30d',
                        'position' => ['x' => 0, 'y' => 0, 'w' => 6, 'h' => 4]
                    ],
                    [
                        'id' => 'user-growth',
                        'type' => 'chart',
                        'title' => 'User Growth',
                        'metric' => 'new_users',
                        'category' => 'users',
                        'chartType' => 'line',
                        'timeRange' => '30d',
                        'position' => ['x' => 6, 'y' => 0, 'w' => 6, 'h' => 4]
                    ],
                    [
                        'id' => 'traffic-overview',
                        'type' => 'chart',
                        'title' => 'Traffic Overview',
                        'metric' => 'unique_visitors',
                        'category' => 'traffic',
                        'chartType' => 'area',
                        'timeRange' => '30d',
                        'position' => ['x' => 0, 'y' => 4, 'w' => 6, 'h' => 4]
                    ],
                    [
                        'id' => 'content-performance',
                        'type' => 'chart',
                        'title' => 'Content Performance',
                        'metric' => 'content_engagement',
                        'category' => 'content',
                        'chartType' => 'bar',
                        'timeRange' => '30d',
                        'position' => ['x' => 6, 'y' => 4, 'w' => 6, 'h' => 4]
                    ]
                ],
                'filters' => [
                    'timeRange' => '30d',
                    'sources' => ['wordpress', 'woocommerce', 'google_analytics'],
                    'categories' => ['sales', 'users', 'traffic', 'content']
                ]
            ])
        ];
    }
}
