<?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\Sanctum\HasApiTokens;
use App\Notifications\ResetPasswordNotification;
use Illuminate\Contracts\Auth\MustVerifyEmail;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'role',
        'phone_number',
        'address',
        'shop_description',
        'profile_photo',
        'latitude',
        'longitude',
    ];

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

    /**
     * Get the attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
        'id' => 'integer',
        'latitude' => 'float',
        'longitude' => 'float',
    ];

    /**
     * Check if user is admin
     */
    public function isAdmin(): bool
    {
        return $this->role === 'admin';
    }

    /**
     * Check if user is penjahit
     */
    public function isPenjahit(): bool
    {
        return $this->role === 'penjahit';
    }

    /**
     * Check if user is pelanggan
     */
    public function isPelanggan(): bool
    {
        return $this->role === 'pelanggan';
    }

    /**
     * Check if user has specific role
     */
    public function hasRole(string $role): bool
    {
        return $this->role === $role;
    }

    /**
     * Get the specializations for the tailor.
     */
    public function specializations()
    {
        return $this->belongsToMany(TailorSpecialization::class, 'tailor_specialization_user');
    }

    /**
     * Get the preferred specializations for the customer.
     */
    public function preferredSpecializations()
    {
        return $this->belongsToMany(TailorSpecialization::class, 'customer_specialization');
    }

    /**
     * Get the services for the tailor.
     */
    public function services()
    {
        return $this->hasMany(TailorService::class, 'user_id');
    }

    /**
     * Get the bookings for the user (as customer).
     */
    public function customerBookings()
    {
        return $this->hasMany(Booking::class, 'customer_id');
    }

    /**
     * Get the bookings for the user (as tailor).
     */
    public function bookings()
    {
        return $this->hasMany(Booking::class, 'tailor_id');
    }

    /**
     * Get the gallery items for the tailor.
     */
    public function gallery()
    {
        return $this->hasMany(TailorGallery::class, 'user_id');
    }

    /**
     * Get the ratings for the tailor.
     */
    public function ratings()
    {
        return $this->hasMany(TailorRating::class, 'tailor_id');
    }

    /**
     * Get the ratings given by the customer.
     */
    public function givenRatings()
    {
        return $this->hasMany(TailorRating::class, 'customer_id');
    }

    /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPasswordNotification($token));
    }

    public function wallet()
    {
        return $this->hasOne(Wallet::class);
    }

    public function bankAccounts()
    {
        return $this->hasMany(BankAccount::class);
    }

    /**
     * Kirim notifikasi verifikasi email custom
     */
    public function sendEmailVerificationNotification()
    {
        // Generate verification URL yang langsung mengarah ke backend
        $verificationUrl = url()->temporarySignedRoute(
            'verification.verify',
            now()->addMinutes(60),
            ['id' => $this->id, 'hash' => sha1($this->email)]
        );
        
        $this->notify(new \App\Notifications\VerifyEmailNotification($verificationUrl));
    }
}
