<?php

namespace App\Models;

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

class ShopContact extends Model
{
    protected $table = 'shop_contacts';

    protected $fillable = [
        'shop_id',
        'code',
        'contact_type',
        'name',
        'phone',
        'address',
        'credit_limit',
        'branch_code',
        'local_db_created_at',
    ];

    protected $casts = [
        'credit_limit' => 'decimal:2',
        'local_db_created_at' => 'datetime',
    ];

    public function shop()
    {
        return $this->belongsTo(Account::class, 'shop_id')->where('model_type', 'shop');
    }

    public function transactions()
    {
        return $this->hasMany(ShopContactAccountTransaction::class, 'contact_code', 'code')
            ->where('shop_id', $this->shop_id)
            ->orderBy('local_db_created_at', 'desc');
    }
}
