<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Booking;
use App\Models\Payment;
use App\Models\Property;
use App\Models\Report;
use App\Models\User;
use Illuminate\Http\Request;
use Inertia\Inertia;

class AdminController extends Controller
{
    public function login()
    {
        return Inertia::render('Admin/Auth/Login');
    }

    public function dashboard()
    {
        $summaryCards = [
            [
                'name' => 'Total Users',
                'value' => User::count(),
                'icon' => 'fa-users',
            ],
            [
                'name' => 'Verified Properties',
                'value' => Property::where('is_verified', true)->count(),
                'icon' => 'fa-home',
            ],
            [
                'name' => 'Pending Reports',
                'value' => Report::pending()->count(),
                'icon' => 'fa-flag',
            ],
            [
                'name' => 'Overdue Payments',
                'value' => Payment::where('status', 1)
                    ->whereDate('due_date', '<', now())
                    ->count(),
                'icon' => 'fa-credit-card',
            ],
            [
                'name' => 'Bookings Today',
                'value' => Booking::whereDate('created_at', now()->toDateString())->count(),
                'icon' => 'fa-calendar',
            ],
            [
                'name' => 'Pending Landlords',
                'value' => User::where('role_type', 1)->where('status', 2)->count(),
                'icon' => 'fa-id-badge',
            ],
        ];

        $propertyStatusCounts = [
            'available' => Property::where('status', 1)->count(),
            'occupied' => Property::where('status', 2)->count(),
            'pending' => Property::where('status', 3)->count(),
            'maintenance' => Property::where('status', 4)->count(),
        ];

        $pendingLandlords = User::where('role_type', 1)
            ->where('status', 2)
            ->latest()
            ->take(5)
            ->get(['id', 'name', 'email', 'created_at'])
            ->map(fn ($user) => [
                'id' => $user->id,
                'name' => $user->name,
                'email' => $user->email,
                'created_at' => $user->created_at?->format('Y-m-d'),
            ]);

        $recentReports = Report::latest()
            ->take(5)
            ->get(['id', 'reason', 'status', 'created_at'])
            ->map(fn ($report) => [
                'id' => $report->id,
                'reason' => $report->reason,
                'status' => $report->status_name ?? Report::getStatusOptions()[$report->status] ?? 'Unknown',
                'created_at' => $report->created_at?->format('Y-m-d'),
            ]);

        return Inertia::render('Admin/Dashboard', [
            'stats' => [
                'summary_cards' => $summaryCards,
                'property_status_counts' => $propertyStatusCounts,
                'pending_landlords' => $pendingLandlords,
                'recent_reports' => $recentReports,
            ],
        ]);
    }

    public function authenticate(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
            'password' => 'required',
        ]);

        if (auth()->attempt($request->only('email', 'password'))) {
            $request->session()->regenerate();
            return redirect()->intended('/');
        }

        return back()->withErrors([
            'message' => 'The provided credentials do not match our records.',
        ]);
    }

    public function logout(Request $request)
    {
        auth()->logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();
        
        return redirect()->route('admin.login');
    }
}
