<?php

namespace App\Models;

use App\Providers\RouteServiceProvider;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Wave\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'username',
        'password',
        'verification_code',
        'verified',
        'trial_ends_at',
        'company_id',
        'first_name',
        'last_name',
        'alias_name',
        'phone',
        'forgot_password_otp',
        'status',
        'role_id',
        'api_token',
        'admin_active_company_id'
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'trial_ends_at' => 'datetime',
    ];

    public function getRedirectRoute()
    {
        switch ($this->role_id) {
            case Role::OWNER_ROLE:
                return RouteServiceProvider::USER_HOME;
            case Role::ADMINISTRATOR_ROLE:
                return RouteServiceProvider::USER_HOME;
            case Role::BOOKKEEPER_ROLE:
                return RouteServiceProvider::USER_HOME;
            case Role::COMPANY_SECRETARY_ROLE:
                return RouteServiceProvider::USER_HOME;
            case Role::IT_PERSONNEL_ROLE:
                return RouteServiceProvider::ADMIN_HOME;
            case Role::NUMSTATION_MANAGER_ROLE:
                return RouteServiceProvider::ADMIN_HOME;
            case Role::NUMSTATION_STAFF_ROLE:
                return RouteServiceProvider::ADMIN_HOME;
            default:
                return RouteServiceProvider::USER_HOME;
        }
    }

    public function getRedirectRouteIfNotAuthenticated()
    {
        switch ($this->role_id) {
            case Role::OWNER_ROLE:
                return RouteServiceProvider::USER_LOGIN;
            case Role::ADMINISTRATOR_ROLE:
                return RouteServiceProvider::USER_LOGIN;
            case Role::BOOKKEEPER_ROLE:
                return RouteServiceProvider::USER_LOGIN;
            case Role::COMPANY_SECRETARY_ROLE:
                return RouteServiceProvider::USER_LOGIN;
            case Role::IT_PERSONNEL_ROLE:
                return RouteServiceProvider::ADMIN_LOGIN;
            case Role::NUMSTATION_MANAGER_ROLE:
                return RouteServiceProvider::ADMIN_LOGIN;
            case Role::NUMSTATION_STAFF_ROLE:
                return RouteServiceProvider::ADMIN_LOGIN;
            default:
                return RouteServiceProvider::USER_LOGIN;
        }
    }

    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    public function notificationSettings()
    {
        return $this->hasMany(UserNotificationSetting::class);
    }

    public function userRole()
    {
        return $this->hasOne(Role::class, 'id', 'role_id');
    }

    public function notifications()
    {
        return $this->hasMany(UserNotification::class);
    }

    public function adminActiveCompany()
    {
        return $this->hasOne(Company::class, 'id', 'admin_active_company_id');
    }
}
