<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use PHPOpenSourceSaver\JWTAuth\Contracts\JWTSubject;

class Vendor extends Authenticatable implements JWTSubject
{
    use HasFactory, Notifiable, SoftDeletes;

    protected $fillable = [
        'application_id',
        'store_name',
        'slug',
        'description',
        'logo',
        'banner',
        'phone',
        'email',
        'password',
        'pin',
        'address',
        'locality',
        'lat',
        'lng',
        'category_id',
        'tier',
        'commission_rate',
        'uses_platform_delivery',
        'uses_self_delivery',
        'min_order_value',
        'delivery_zones',
        'settlement_method',
        'settlement_account_name',
        'settlement_account_number',
        'status',
    ];

    protected $hidden = [
        'password',
        'pin',
        'remember_token',
        'settlement_account_number',
    ];

    protected function casts(): array
    {
        return [
            'password' => 'hashed',
            'pin' => 'hashed',
            'commission_rate' => 'decimal:2',
            'fulfilment_rate' => 'decimal:2',
            'rating_average' => 'decimal:2',
            'pending_settlement' => 'decimal:2',
            'min_order_value' => 'decimal:2',
            'lat' => 'decimal:7',
            'lng' => 'decimal:7',
            'uses_platform_delivery' => 'boolean',
            'uses_self_delivery' => 'boolean',
            'trial_period_complete' => 'boolean',
            'delivery_zones' => 'array',
        ];
    }

    public function getJWTIdentifier(): mixed
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims(): array
    {
        return ['role' => 'vendor', 'tier' => $this->tier];
    }

    public function application(): BelongsTo
    {
        return $this->belongsTo(VendorApplication::class, 'application_id');
    }

    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }

    public function products(): HasMany
    {
        return $this->hasMany(Product::class);
    }

    public function orderItems(): HasMany
    {
        return $this->hasMany(OrderItem::class);
    }

    public function settlements(): HasMany
    {
        return $this->hasMany(Settlement::class);
    }

    public function ratings(): HasMany
    {
        return $this->morphMany(Rating::class, 'rateable');
    }

    public function complaints(): HasMany
    {
        return $this->hasMany(Complaint::class);
    }

    public function isActive(): bool
    {
        return $this->status === 'active';
    }

    public function getEffectiveCommissionRate(): float
    {
        return (float) $this->commission_rate;
    }
}
