<?php

namespace App\Core\Chart;

/**
 * Chart Generator - مولد الرسوم البيانية
 * يدعم Charts.js format
 */
class ChartGenerator
{
    /**
     * Generate line chart data
     */
    public static function lineChart(array $labels, array $datasets, array $options = []): array
    {
        return [
            'type' => 'line',
            'data' => [
                'labels' => $labels,
                'datasets' => $datasets
            ],
            'options' => array_merge([
                'responsive' => true,
                'maintainAspectRatio' => false,
                'plugins' => [
                    'legend' => ['display' => true],
                    'tooltip' => ['enabled' => true]
                ]
            ], $options)
        ];
    }

    /**
     * Generate bar chart data
     */
    public static function barChart(array $labels, array $datasets, array $options = []): array
    {
        return [
            'type' => 'bar',
            'data' => [
                'labels' => $labels,
                'datasets' => $datasets
            ],
            'options' => array_merge([
                'responsive' => true,
                'maintainAspectRatio' => false,
                'plugins' => [
                    'legend' => ['display' => true]
                ]
            ], $options)
        ];
    }

    /**
     * Generate pie chart data
     */
    public static function pieChart(array $labels, array $data, array $colors = [], array $options = []): array
    {
        $defaultColors = [
            '#0d9488', '#14b8a6', '#5eead4',
            '#f59e0b', '#f97316', '#ef4444',
            '#8b5cf6', '#a855f7', '#d946ef'
        ];

        $colors = !empty($colors) ? $colors : $defaultColors;

        return [
            'type' => 'pie',
            'data' => [
                'labels' => $labels,
                'datasets' => [[
                    'data' => $data,
                    'backgroundColor' => array_slice($colors, 0, count($data))
                ]]
            ],
            'options' => array_merge([
                'responsive' => true,
                'maintainAspectRatio' => false,
                'plugins' => [
                    'legend' => ['display' => true, 'position' => 'right']
                ]
            ], $options)
        ];
    }

    /**
     * Generate revenue chart data
     */
    public static function revenueChart(array $dates, array $amounts, string $currency = 'IQD'): array
    {
        return self::lineChart($dates, [[
            'label' => 'الإيرادات (' . $currency . ')',
            'data' => $amounts,
            'borderColor' => '#0d9488',
            'backgroundColor' => 'rgba(13, 148, 136, 0.1)',
            'fill' => true,
            'tension' => 0.4
        ]], [
            'scales' => [
                'y' => [
                    'beginAtZero' => true,
                    'ticks' => [
                        'callback' => 'function(value) { return value.toLocaleString() + " ' . $currency . '"; }'
                    ]
                ]
            ]
        ]);
    }

    /**
     * Generate test distribution chart
     */
    public static function testDistributionChart(array $testNames, array $counts): array
    {
        return self::barChart($testNames, [[
            'label' => 'عدد الطلبات',
            'data' => $counts,
            'backgroundColor' => '#0d9488'
        ]]);
    }
}
