<?php

namespace App\Traits\Components;

use Illuminate\Http\RedirectResponse;

trait WithRoleRedirect
{
    public function goToProfile(): RedirectResponse
    {
        if (! auth()->check()) {
            return redirect('/login');
        }

        $user = auth()->user();
        $roles = $user->roles->pluck('name')->toArray();

        if (array_intersect($roles, ['Developer', 'Owner', 'Leader', 'Admin'])) {
            return redirect('/studio/dashboard/overview');
        }

        if (in_array('Partner', $roles, true)) {
            return redirect('/partner/overview');
        }

        if (in_array('Customer', $roles, true)) {
            return redirect('/member/overview');
        }

        return redirect('/login');
    }
}
