<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Article;
use App\Services\DashboardStatsService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class DashboardController extends BaseController
{
    public function __construct(
        private DashboardStatsService $dashboardStatsService
    ) {}

    /**
     * Get dashboard statistics
     */
    public function stats(Request $request): JsonResponse
    {
        $period = $request->get('period', 'today');

        try {
            // Get article stats from service
            $articleStats = $this->dashboardStatsService->getStats($period);

            // Get system stats
            $systemStats = $this->dashboardStatsService->getSystemStats();

            // Get available periods
            $availablePeriods = $this->dashboardStatsService->getAvailablePeriods();

            return $this->sendResponse([
                'article_stats' => $articleStats,
                'system_stats' => $systemStats,
                'available_periods' => $availablePeriods,
                'current_period' => $period,
            ]);
        } catch (\Exception $e) {
            return $this->sendError('Failed to fetch dashboard stats: '.$e->getMessage(), [], 500);
        }
    }
}
