<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Str;

/**
 * Vendor Service Model
 * Links vendors to existing service items with custom pricing and features
 *
 * @property string $id
 * @property string $vendor_id
 * @property string $service_item_id
 * @property string $name
 * @property string $slug
 * @property string|null $description
 * @property float $price
 * @property decimal $min_order_quantity
 * @property decimal $max_order_quantity
 * @property int $production_days
 * @property string|null $notes
 * @property bool $is_active
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property-read \App\Models\Vendor $vendor
 * @property-read \App\Models\ServiceItem $serviceItem
 * @property-read \Illuminate\Database\Eloquent\Collection<VendorServiceFeature> $features
 * @property-read \Illuminate\Database\Eloquent\Collection<ServiceOrder> $orders
 * @method static \Illuminate\Database\Eloquent\Builder<VendorService>|VendorService newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<VendorService>|VendorService query()
 */
class VendorService extends Model
{
    use HasFactory, SoftDeletes;

    /**
     * Indicates if IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    /**
     * The data type of auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'string';

    protected $fillable = [
        'vendor_id',
        'service_item_id',
        'name',
        'slug',
        'description',
        'price',
        'min_order_quantity',
        'max_order_quantity',
        'production_days',
        'notes',
        'is_active',
    ];

    protected $casts = [
        'price' => 'decimal:2',
        'min_order_quantity' => 'decimal:2',
        'max_order_quantity' => 'decimal:2',
        'production_days' => 'integer',
        'is_active' => 'boolean',
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($vendorService) {
            if (empty($vendorService->id)) {
                $vendorService->id = (string) Str::uuid();
            }
            if (empty($vendorService->slug)) {
                $vendorService->slug = Str::slug($vendorService->name);
            }
        });
    }

    /**
     * Get the vendor that owns the service.
     */
    public function vendor(): BelongsTo
    {
        return $this->belongsTo(Vendor::class, 'vendor_id');
    }

    /**
     * Get the service item this vendor service is based on.
     */
    public function serviceItem(): BelongsTo
    {
        return $this->belongsTo(ServiceItem::class, 'service_item_id');
    }

    /**
     * Get the features for this vendor service.
     */
    public function features(): HasMany
    {
        return $this->hasMany(VendorServiceFeature::class, 'vendor_service_id');
    }

    /**
     * Get the orders for this vendor service.
     */
    public function orders(): HasMany
    {
        return $this->hasMany(ServiceOrder::class, 'vendor_service_id');
    }

    /**
     * Scope to get only active services.
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Scope to get services by vendor.
     */
    public function scopeByVendor($query, $vendorId)
    {
        return $query->where('vendor_id', $vendorId);
    }

    /**
     * Scope to get services by service item.
     */
    public function scopeByServiceItem($query, $serviceItemId)
    {
        return $query->where('service_item_id', $serviceItemId);
    }

    /**
     * Get the formatted price.
     */
    public function getFormattedPriceAttribute()
    {
        return '₦' . number_format($this->price, 2);
    }

    /**
     * Get the service category through service item.
     */
    public function getCategoryAttribute()
    {
        return $this->serviceItem?->serviceCategory?->name;
    }
}
