<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AlertRule;
use App\Models\Client;
use App\Models\GeneratedReport;
use App\Models\PlatformConnection;
use App\Models\ReportSchedule;

class DashboardController extends Controller
{
    public function index()
    {
        $page_title = 'Dashboard';
        $page_description = 'DW Ads Reporter genel bakış';

        $stats = [
            'clients' => Client::count(),
            'active_clients' => Client::where('status', 'active')->count(),
            'connections' => PlatformConnection::count(),
            'google_connections' => PlatformConnection::where('platform', 'google_ads')->count(),
            'meta_connections' => PlatformConnection::where('platform', 'meta_ads')->count(),
            'gsc_connections' => PlatformConnection::where('platform', 'search_console')->count(),
            'scheduled_reports' => ReportSchedule::where('is_active', true)->count(),
            'reports' => GeneratedReport::count(),
            'alerts' => AlertRule::where('is_active', true)->count(),
        ];

        $latestClients = Client::latest()->limit(5)->get();
        $latestReports = GeneratedReport::with('client')->latest()->limit(5)->get();
        $connections = PlatformConnection::with('client')->latest()->limit(6)->get();

        return view('admin.dashboard.index', compact(
            'page_title',
            'page_description',
            'stats',
            'latestClients',
            'latestReports',
            'connections'
        ));
    }
}
