<?php

namespace App\Models;

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

class PriceListingSetup extends Model
{
    use HasFactory;

    protected $table = 'price_listing_setups1';

    protected $fillable = [
        'code',
        'description1',
        'description2',
        'active',
        'is_service_item',
        'is_physical_item',
        'has_warehouse',
        'has_units_of_measure',
        'category_id',
        'bin_location_id',
        'barcode_box',
        'cost',
        'least_grv_cost',
        'min_gp_pct',
        'vat_rate',
        'warehouse_id',
        'warehouse_desc',
        'original_quantity',
        'stocking_unit_id',
        'default_purchase_unit_id',
        'default_sale_unit_id',
    ];

    protected $casts = [
        'active'               => 'boolean',
        'is_service_item'      => 'boolean',
        'is_physical_item'     => 'boolean',
        'has_warehouse'        => 'boolean',
        'has_units_of_measure' => 'boolean',
        'cost'                 => 'decimal:2',
        'least_grv_cost'       => 'decimal:2',
        'min_gp_pct'           => 'decimal:2',
        'vat_rate'             => 'decimal:2',
        'original_quantity'    => 'integer',
    ];

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

    public function binLocation(): BelongsTo
    {
        return $this->belongsTo(BinLocation::class, 'bin_location_id');
    }

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

    public function priceMarkups(): HasMany
    {
        return $this->hasMany(PriceListingMarkup::class, 'price_listing_setup_id');
    }

    public function itemWarehouses()
    {
        return $this->hasMany(ItemWarehouse::class, 'item_id', 'id');
    }


    public function images(): HasMany
    {
        return $this->hasMany(ItemTypeImage::class, 'price_listing_setup_id');
    }

    public function stockingUnit(): BelongsTo
    {
        return $this->belongsTo(Uom::class, 'stocking_unit_id');
    }

    public function purchaseUnit(): BelongsTo
    {
        return $this->belongsTo(Uom::class, 'default_purchase_unit_id');
    }

    public function saleUnit(): BelongsTo
    {
        return $this->belongsTo(Uom::class, 'default_sale_unit_id');
    }
}
