<?php

namespace App\Http\Controllers\Api\V1\Immigopro;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Portal;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ImmigoProUserController extends Controller
{
    /**
     * Get all general users with ImmiGoPro portal access
     */
    public function index(Request $request): JsonResponse
    {
        // Get ImmiGoPro portal
        $immigoproPortal = Portal::where('key', 'immigopro')->first();
        
        if (!$immigoproPortal) {
            return response()->json([
                'success' => false,
                'message' => 'ImmiGoPro portal not found',
                'data' => []
            ]);
        }

        // Get users who have access to ImmiGoPro portal
        // Exclude super admins and marketing officers
        $users = User::whereHas('portals', function ($query) use ($immigoproPortal) {
                $query->where('portal_id', $immigoproPortal->id)
                      ->where('role_scope', 'user'); // Only general users
            })
            ->whereDoesntHave('roles', function ($query) {
                $query->whereIn('name', ['super_admin', 'marketing_officer']);
            })
            ->with(['portals' => function ($query) use ($immigoproPortal) {
                $query->where('portal_id', $immigoproPortal->id);
            }])
            ->orderBy('created_at', 'desc')
            ->get()
            ->map(function ($user) use ($immigoproPortal) {
                $portalUser = $user->portals->first();
                
                return [
                    'id' => $user->id,
                    'name' => $user->name,
                    'email' => $user->email,
                    'phone' => $user->phone,
                    'avatar' => $user->avatar,
                    'avatar_url' => $user->avatar_url,
                    'status' => $user->status,
                    'role_scope' => $portalUser?->pivot->role_scope ?? 'user',
                    'portal_access_granted_at' => $portalUser?->pivot->created_at,
                    'created_at' => $user->created_at,
                ];
            });

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

    /**
     * Get a specific user's details
     */
    public function show(int $id): JsonResponse
    {
        $immigoproPortal = Portal::where('key', 'immigopro')->first();
        
        $user = User::whereHas('portals', function ($query) use ($immigoproPortal) {
                $query->where('portal_id', $immigoproPortal->id)
                      ->where('role_scope', 'user');
            })
            ->with(['portals' => function ($query) use ($immigoproPortal) {
                $query->where('portal_id', $immigoproPortal->id);
            }])
            ->findOrFail($id);

        $portalUser = $user->portals->first();

        return response()->json([
            'success' => true,
            'data' => [
                'id' => $user->id,
                'name' => $user->name,
                'email' => $user->email,
                'phone' => $user->phone,
                'address' => $user->address,
                'avatar' => $user->avatar,
                'avatar_url' => $user->avatar_url,
                'status' => $user->status,
                'role_scope' => $portalUser?->pivot->role_scope ?? 'user',
                'portal_access_granted_at' => $portalUser?->pivot->created_at,
                'created_at' => $user->created_at,
                'email_verified_at' => $user->email_verified_at,
            ]
        ]);
    }
}
