<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class RedirectDashboardController extends Controller
{
    public function __invoke()
    {
        $user = auth()->user();

        if ($user->isAdmin()) {
            return redirect()->route('admin.dashboard');
        }

        // Check activation for Leasing and Member
        if (!$user->isActive()) {
            if ($user->isLeasing()) {
                // If leasing has not selected plan, go to plans.
                // If already selected, go to payment.
                if (!$user->subscriptions()->where('subscription_status', 'inactive')->exists()) {
                    return redirect()->route('leasing.plans.index');
                }
                return redirect()->route('leasing.payment.show');
            }
            
            if ($user->isMember()) {
                if (!$user->subscriptions()->where('subscription_status', 'inactive')->exists()) {
                    return redirect()->route('member.plans.index');
                }
                return redirect()->route('member.payment.show');
            }

            return redirect()->route('payment.required');
        }

        if ($user->isLeasing()) {
            return redirect()->route('leasing.dashboard');
        }
        
        if ($user->isMember()) {
            return redirect()->route('member.dashboard');
        }

        return redirect()->route('payment.required');
    }
}
