<?php

namespace Modules\SystemCore\App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class CoreSalesOrderLine extends Model
{
    use SoftDeletes;

    protected $table = 'core_sales_order_lines';

    protected $fillable = [
        'sales_order_id',
        'product_id',
        'product_variant_id',
        'unit_id',
        'product_name',
        'product_sku',
        'quantity',
        'weight',
        'unit_price',
        'unit_cost',
        'tax_rate',
        'tax_amount',
        'discount_rate',
        'discount_amount',
        'subtotal',
        'line_total',
        'profit',
        'fulfilled_quantity',
        'is_fulfilled',
        'returned_quantity',
        'return_reason',
        'created_by_id',
        'updated_by_id',
    ];

    protected function casts(): array
    {
        return [
            'quantity' => 'decimal:2',
            'weight' => 'decimal:2',
            'unit_price' => 'decimal:4',
            'unit_cost' => 'decimal:4',
            'tax_rate' => 'decimal:2',
            'tax_amount' => 'decimal:2',
            'discount_rate' => 'decimal:2',
            'discount_amount' => 'decimal:2',
            'subtotal' => 'decimal:2',
            'line_total' => 'decimal:2',
            'profit' => 'decimal:2',
            'is_fulfilled' => 'boolean',
        ];
    }

    /**
     * Get sales order
     */
    public function salesOrder(): BelongsTo
    {
        return $this->belongsTo(CoreSalesOrder::class, 'sales_order_id');
    }

    /**
     * Get product
     */
    public function product(): BelongsTo
    {
        return $this->belongsTo(CoreProduct::class, 'product_id');
    }

    /**
     * Get product variant
     */
    public function productVariant(): BelongsTo
    {
        return $this->belongsTo(CoreProductVariant::class, 'product_variant_id');
    }

    /**
     * Get tax configuration
     */
    public function taxConfiguration(): BelongsTo
    {
        return $this->belongsTo(CoreTaxConfiguration::class, 'tax_id');
    }
}
