<?php

namespace App\Livewire\Admin;

use Livewire\Component;
use Livewire\Attributes\Layout;
use App\Models\ContactLead;
use App\Models\Blog;
use App\Models\Work;
use App\Models\Menu;
use Carbon\Carbon;

#[Layout('layouts.admin')]
class Dashboard extends Component
{
    public function render()
    {
        // ================== MONTHLY STATS ==================
        $lastMonthSubmissions = ContactLead::whereMonth('created_at', now()->subMonth()->month)->count();
        $thisMonthSubmissions = ContactLead::whereMonth('created_at', now()->month)->count();

        $submissionsGrowth = $lastMonthSubmissions > 0
            ? round((($thisMonthSubmissions - $lastMonthSubmissions) / $lastMonthSubmissions) * 100, 1)
            : 100;

        $monthlyBlogs = Blog::whereMonth('created_at', now()->month)->count();
        $pendingPages = Menu::where('status', 0)->count();

        // ================== COUNTS ==================
        $totalSubmissions = ContactLead::count();
        $totalBlogs       = Blog::where('status', 1)->count();
        $totalPages       = Menu::where('status', 1)->count();
        $totalWorks       = Work::count();

        $activeContent = $totalBlogs + $totalPages + $totalWorks;

        $monthlyContent =
            Blog::whereMonth('created_at', now()->month)->count() +
            Menu::whereMonth('created_at', now()->month)->count() +
            Work::whereMonth('created_at', now()->month)->count();

        // ================== USER METRICS ==================
        $formCount = ContactLead::count();
        $workCount = Work::count();

        $conversionRate = $formCount > 0
            ? round(($workCount / $formCount) * 100, 1)
            : 0;

        $avgResponseTime = round(rand(8, 20) / 10, 1); // realistic mock 0.8s - 2.0s

        // ================== CHART DATA (Last 6 Months) ==================
        $chartLabels = [];
        $chartData = [];

        for ($i = 5; $i >= 0; $i--) {
            $month = Carbon::now()->subMonths($i);
            $chartLabels[] = $month->format('M');

            $count =
                ContactLead::whereMonth('created_at', $month->month)->count() +
                Blog::whereMonth('created_at', $month->month)->count() +
                Work::whereMonth('created_at', $month->month)->count();

            $chartData[] = $count;
        }

        // ================== UNIFIED RECENT ACTIVITY ==================
        $activities = collect();

        if ($latestForm = ContactLead::latest()->first()) {
            $activities->push([
                'type' => 'form',
                'title' => 'New form submitted',
                'subtitle' => 'Lead form',
                'icon' => 'send',
                'color' => 'orange',
                'time' => $latestForm->created_at,
            ]);
        }

        if ($latestBlog = Blog::latest()->first()) {
            $activities->push([
                'type' => 'blog',
                'title' => 'Blog published',
                'subtitle' => 'Content update',
                'icon' => 'file-text',
                'color' => 'blue',
                'time' => $latestBlog->created_at,
            ]);
        }

        if ($latestWork = Work::latest()->first()) {
            $activities->push([
                'type' => 'work',
                'title' => 'New work added',
                'subtitle' => 'Portfolio update',
                'icon' => 'briefcase',
                'color' => 'emerald',
                'time' => $latestWork->created_at,
            ]);
        }

        $activities = $activities->sortByDesc('time')->values();

        // ================== RETURN ==================
        return view('livewire.admin.dashboard', compact(
            'totalSubmissions',
            'totalBlogs',
            'totalPages',
            'totalWorks',
            'activeContent',
            'monthlyContent',
            'submissionsGrowth',
            'monthlyBlogs',
            'pendingPages',
            'conversionRate',
            'avgResponseTime',
            'chartLabels',
            'chartData',
            'activities'
        ));
    }
}
