<?php

declare(strict_types=1);

namespace App\Models\Market;

use App\Models\Model;
use App\Models\User;

class ProductTransaction extends Model
{
    protected ?string $table = 'prd_trx';

    /**
     * The primary key for the model.
     */
    protected string $primaryKey = 'id';

    /**
     * Indicates if the IDs are auto-incrementing.
     */
    public bool $incrementing = true;

    /**
     * The "type" of the primary key.
     */
    protected string $keyType = 'int';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = [
        'hashkey',
        'created_by',
        'updated_by',
        'created_for',
        'store_id',
        'transactiontype',
        'product_id',
        'transactiondata',
        'description',
        'subtype',
        'name',
        'owner_id',
        'transactionsessionhash',
        'quantity',
        'logs',
        'remarks',
        'price',
        'is_void',
        'last_total_price',
        'last_total_discount',
        'notes'
    ];

    /**
     * The attributes that should be cast.
     */
    protected array $casts = [
        'quantity' => 'integer',
        'price' => 'integer',
        'is_void' => 'boolean',
    ];

    /**
     * Relationships.
     */
    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id');
    }

    public function store()
    {
        return $this->belongsTo(Store::class, 'store_id');
    }

    public function owner()
    {
        return $this->belongsTo(User::class, 'owner_id');
    }

    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function updater()
    {
        return $this->belongsTo(User::class, 'updated_by');
    }

    public function createdFor()
    {
        return $this->belongsTo(User::class, 'created_for');
    }

    public function session()
    {
        return $this->belongsTo(ProductTransactionSession::class, 'transactionsessionhash', 'hashkey');
    }
}
