<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Services\AuthService;
use App\Services\UserService;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
    protected AuthService $authService;
    protected UserService $userService;

    public function __construct(AuthService $authService, UserService $userService)
    {
        $this->authService = $authService;
        $this->userService = $userService;
    }

    /**
     * Get user profile for dashboard
     */
    public function profile(Request $request)
    {
        $user = $request->user();
        $profileData = $this->authService->formatUserProfile($user);
        
        return response()->json([
            'success' => true,
            'data' => $profileData
        ]);
    }

    /**
     * Dashboard analytics
     */
    public function analytics(Request $request)
    {
        $user = $request->user();
        
        if (!$user->hasRole('admin')) {
            return response()->json([
                'success' => false,
                'message' => 'Access denied. Admin privileges required.'
            ], 403);
        }

        $pendingLandlords = $this->userService->getPendingLandlords();
        
        return response()->json([
            'success' => true,
            'data' => [
                'pending_landlords_count' => count($pendingLandlords),
                'pending_landlords' => $pendingLandlords,
            ]
        ]);
    }

    /**
     * Get properties for dashboard
     */
    public function properties(Request $request)
    {
        $user = $request->user();
        
        if ($user->hasRole('landlord')) {
            $properties = $user->properties;
        } elseif ($user->hasRole('admin')) {
            $properties = \App\Models\Property::all();
        } else {
            return response()->json([
                'success' => false,
                'message' => 'Access denied.'
            ], 403);
        }

        return response()->json([
            'success' => true,
            'data' => $properties
        ]);
    }
}