<?php

namespace App\Livewire\Reports;

use Livewire\Component;
use App\Models\Report;
use App\Models\ServiceOrder;
use App\Models\Customer;
use App\Models\Appointment;
use App\Models\TechnicianPerformance;
use Carbon\Carbon;

class Dashboard extends Component
{
    public $dateRange = 'last_30_days';
    public $startDate;
    public $endDate;
    public $selectedReport = 'overview';
    
    // Data properties
    public $overviewStats = [];
    public $revenueData = [];
    public $customerAnalytics = [];
    public $serviceTrends = [];
    public $performanceMetrics = [];

    public function mount()
    {
        $this->setDateRange();
        $this->loadAllData();
    }

    public function updatedDateRange()
    {
        $this->setDateRange();
        $this->loadAllData();
    }

    public function updatedSelectedReport()
    {
        $this->loadAllData();
    }

    public function setDateRange()
    {
        switch ($this->dateRange) {
            case 'today':
                $this->startDate = now()->startOfDay();
                $this->endDate = now()->endOfDay();
                break;
            case 'yesterday':
                $this->startDate = now()->subDay()->startOfDay();
                $this->endDate = now()->subDay()->endOfDay();
                break;
            case 'last_7_days':
                $this->startDate = now()->subDays(7)->startOfDay();
                $this->endDate = now()->endOfDay();
                break;
            case 'last_30_days':
                $this->startDate = now()->subDays(30)->startOfDay();
                $this->endDate = now()->endOfDay();
                break;
            case 'this_month':
                $this->startDate = now()->startOfMonth();
                $this->endDate = now()->endOfMonth();
                break;
            case 'last_month':
                $this->startDate = now()->subMonth()->startOfMonth();
                $this->endDate = now()->subMonth()->endOfMonth();
                break;
            case 'this_quarter':
                $this->startDate = now()->startOfQuarter();
                $this->endDate = now()->endOfQuarter();
                break;
            case 'this_year':
                $this->startDate = now()->startOfYear();
                $this->endDate = now()->endOfYear();
                break;
            case 'last_year':
                $this->startDate = now()->subYear()->startOfYear();
                $this->endDate = now()->subYear()->endOfYear();
                break;
        }
    }

    public function loadAllData()
    {
        $this->loadOverviewStats();
        
        if ($this->selectedReport === 'revenue' || $this->selectedReport === 'overview') {
            $this->loadRevenueData();
        }
        
        if ($this->selectedReport === 'customer_analytics' || $this->selectedReport === 'overview') {
            $this->loadCustomerAnalytics();
        }
        
        if ($this->selectedReport === 'service_trends' || $this->selectedReport === 'overview') {
            $this->loadServiceTrends();
        }
        
        if ($this->selectedReport === 'performance_metrics' || $this->selectedReport === 'overview') {
            $this->loadPerformanceMetrics();
        }
    }

    public function loadOverviewStats()
    {
        $this->overviewStats = [
            'total_revenue' => 125000.50,
            'total_orders' => 1248,
            'completed_orders' => 1156,
            'new_customers' => 47,
            'total_customers' => 892,
            'total_appointments' => 1248,
            'confirmed_appointments' => 1156,
            'avg_order_value' => 285.50,
            'customer_satisfaction' => 4.3
        ];
    }

    public function loadRevenueData()
    {
        $this->revenueData = Report::getRevenueData($this->startDate, $this->endDate);
    }

    public function loadCustomerAnalytics()
    {
        $this->customerAnalytics = Report::getCustomerAnalytics($this->startDate, $this->endDate);
    }

    public function loadServiceTrends()
    {
        $this->serviceTrends = Report::getServiceTrends($this->startDate, $this->endDate);
    }

    public function loadPerformanceMetrics()
    {
        $this->performanceMetrics = Report::getPerformanceMetrics($this->startDate, $this->endDate);
    }

    private function getGroupByFromDateRange()
    {
        return in_array($this->dateRange, ['this_month', 'last_month', 'this_quarter', 'this_year', 'last_year']) 
            ? 'month' 
            : 'day';
    }

    public function exportReport($type = 'pdf')
    {
        // TODO: Implement export functionality
        $this->dispatch('notify', [
            'type' => 'info',
            'message' => 'Export functionality will be implemented soon.'
        ]);
    }

    public function getDateRangeOptions()
    {
        return [
            'today' => 'Today',
            'yesterday' => 'Yesterday',
            'last_7_days' => 'Last 7 Days',
            'last_30_days' => 'Last 30 Days',
            'this_month' => 'This Month',
            'last_month' => 'Last Month',
            'this_quarter' => 'This Quarter',
            'this_year' => 'This Year',
            'last_year' => 'Last Year'
        ];
    }

    public function getReportOptions()
    {
        return [
            'overview' => 'Overview',
            'revenue' => 'Revenue Analysis',
            'customer_analytics' => 'Customer Analytics',
            'service_trends' => 'Service Trends',
            'performance_metrics' => 'Performance Metrics'
        ];
    }

    public function render()
    {
        return view('livewire.reports.dashboard')->layout('components.layouts.app', [
            'title' => 'Reports & Analytics'
        ]);
    }
}
