<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

#[Fillable(['name', 'email', 'password', 'role', 'credits', 'parent_name', 'parent_email'])]
#[Hidden(['password', 'remember_token'])]
class User extends Authenticatable
{
    /** @use HasFactory<UserFactory> */
    use HasFactory, Notifiable;

    /**
     * Get the user's subscriptions.
     */
    public function subscriptions()
    {
        return $this->hasMany(Subscription::class);
    }

    /**
     * Get the user's active subscription.
     */
    public function activeSubscription()
    {
        return $this->hasOne(Subscription::class)
            ->where('status', 'active')
            ->where('end_date', '>', now())
            ->latest();
    }

    /**
     * Check if the user has an active subscription.
     */
    public function hasActiveSubscription()
    {
        return $this->activeSubscription()->exists();
    }

    /**
     * Get the files uploaded by the user.
     */
    public function files()
    {
        return $this->hasMany(File::class, 'uploaded_by');
    }

    /**
     * Get the file buckets created by the user.
     */
    public function createdBuckets()
    {
        return $this->hasMany(FileBucket::class, 'created_by');
    }

    /**
     * Get the upload sessions for the user.
     */
    public function uploadSessions()
    {
        return $this->hasMany(UploadSession::class);
    }

    /**
     * Get the file permissions for the user.
     */
    public function filePermissions()
    {
        return $this->hasMany(FilePermission::class);
    }

    /**
     * Get the signed URLs created by the user.
     */
    public function createdSignedUrls()
    {
        return $this->hasMany(SignedUrl::class, 'created_by');
    }

    /**
     * Get the storage usage stats for the user.
     */
    public function storageUsageStats()
    {
        return $this->hasMany(StorageUsageStat::class);
    }

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