<?php
// app/Http/Controllers/Admin/DashboardController.php

namespace App\Http\Controllers\Admin;

use App\Models\Content;
use App\Models\Category;
use App\Models\Tag;
use App\Models\Comment;
use Illuminate\Support\Facades\DB;

class CategoryController extends AdminController
{
    public function index()
    {
        $stats = [
            'totalContents' => Content::count(),
            'totalCategories' => Category::count(),
            'totalTags' => Tag::count(),
            'pendingComments' => Comment::where('status', 'pending')->count(),
            'recentActivities' => $this->getRecentActivities(),
            'chartData' => $this->getChartData(),
            'categoryData' => $this->getCategoryDistribution(),
        ];

        return $this->render('Dashboard/Dashboard', [
            'stats' => $stats
        ]);
    }

    private function getRecentActivities()
    {
        // Get recent contents and comments
        $recentContents = Content::with('author')
            ->latest()
            ->take(5)
            ->get()
            ->map(function ($content) {
                return [
                    'type' => 'content',
                    'title' => $content->title,
                    'user' => $content->author?->name ?? 'System',
                    'time' => $content->created_at->diffForHumans(),
                    'icon' => 'file-alt',
                    'color' => 'info'
                ];
            });

        $recentComments = Comment::with('user')
            ->latest()
            ->take(5)
            ->get()
            ->map(function ($comment) {
                return [
                    'type' => 'comment',
                    'title' => 'New comment',
                    'content' => substr($comment->content, 0, 50) . '...',
                    'user' => $comment->author_name,
                    'time' => $comment->created_at->diffForHumans(),
                    'icon' => 'comments',
                    'color' => 'success'
                ];
            });

        return $recentContents->concat($recentComments)->sortByDesc('time')->take(10)->values();
    }

    private function getChartData()
    {
        // Get content creation statistics for last 30 days
        $data = Content::select(
            DB::raw('DATE(created_at) as date'),
            DB::raw('count(*) as count')
        )
        ->where('created_at', '>=', now()->subDays(30))
        ->groupBy('date')
        ->orderBy('date')
        ->get();

        return [
            'labels' => $data->pluck('date')->map(function ($date) {
                return \Carbon\Carbon::parse($date)->format('M d');
            }),
            'datasets' => [
                [
                    'label' => 'Contents Created',
                    'data' => $data->pluck('count'),
                    'borderColor' => 'rgb(75, 192, 192)',
                    'tension' => 0.1
                ]
            ]
        ];
    }

    private function getCategoryDistribution()
    {
        $data = Category::withCount('contents')
            ->having('contents_count', '>', 0)
            ->orderBy('contents_count', 'desc')
            ->take(5)
            ->get();

        return [
            'labels' => $data->pluck('name'),
            'datasets' => [
                [
                    'data' => $data->pluck('contents_count'),
                    'backgroundColor' => [
                        'rgb(255, 99, 132)',
                        'rgb(54, 162, 235)',
                        'rgb(255, 205, 86)',
                        'rgb(75, 192, 192)',
                        'rgb(153, 102, 255)',
                    ]
                ]
            ]
        ];
    }
}
