<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Scout\Searchable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

class User extends Authenticatable
{
    /** @use HasFactory<\Database\Factories\UserFactory> */
    use HasRoles, Searchable, HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'name',
        'email',
        'phone',
        'username',
        'bio',
        'profile_photo',
        'is_active',
        'is_verified',
        'password',
    ];

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

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'password' => 'hashed',
        ];
    }

    // public function roles() {
    //     return $this->belongsToMany(Role::class, 'model_has_roles', 'model_id', 'role_id');
    // }

    public function permissions() {
        return $this->belongsToMany(Permission::class, 'model_has_permissions', 'model_id', 'permission_id');
    }

    public function getProfilePhotoAttribute($value) {
        return $value ? url('uploads/users/' . $value) : null;
    }

    public function studentProfile() {
        return $this->hasOne(StudentProfile::class);
    }

    public function researcherProfile() {
        return $this->hasOne(ResearcherProfile::class);
    }

    public function recruiterProfile() {
        return $this->hasOne(RecruiterProfile::class);
    }

    public function learnerProfile() {
        return $this->hasOne(LearnerProfile::class);
    }

    public function oauthProviders() {
        return $this->hasMany(OAuthProvider::class);
    }

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

    public function examAttempts() {
        return $this->hasMany(ExamAttempt::class);
    }

    public function certificates() {
        return $this->hasMany(Certificate::class);
    }

    public function examBookings() {
        return $this->hasMany(ExamBooking::class);
    }

    public function subscriptions() {
        return $this->hasMany(Subscription::class);
    }

    public function activeSubscription() {
        return $this->hasOne(Subscription::class)->where('status', 'active')
            ->where(function ($query) {
                $query->whereNull('expires_at')
                    ->orWhere('expires_at', '>', now());
            });
    }

    public function paymentMethods() {
        return $this->hasMany(PaymentMethod::class);
    }

    public function billingHistory() {
        return $this->hasMany(BillingHistory::class);
    }

    public function enrollments() {
        return $this->hasMany(CourseEnrollment::class);
    }

    public function activityLogs() {
        return $this->hasMany(UserActivityLog::class);
    }
}
