<?php

namespace App\Models;

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

class LineOwnershipHistory extends Model
{
    use HasFactory;

    protected $table = 'line_ownership_history';

    protected $fillable = [
        'line_id', 'previous_customer_id', 'new_customer_id',
        'previous_main_account_id', 'new_main_account_id',
        'previous_sub_account_id', 'new_sub_account_id',
        'previous_status', 'new_status', 'reason', 'user_id', 'changed_at',
    ];

    protected $casts = [
        'changed_at' => 'datetime',
    ];

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

    public function previousCustomer(): BelongsTo
    {
        return $this->belongsTo(Customer::class, 'previous_customer_id');
    }

    public function newCustomer(): BelongsTo
    {
        return $this->belongsTo(Customer::class, 'new_customer_id');
    }

    public function previousMainAccount(): BelongsTo
    {
        return $this->belongsTo(MainAccount::class, 'previous_main_account_id');
    }

    public function newMainAccount(): BelongsTo
    {
        return $this->belongsTo(MainAccount::class, 'new_main_account_id');
    }

    public function previousSubAccount(): BelongsTo
    {
        return $this->belongsTo(SubAccount::class, 'previous_sub_account_id');
    }

    public function newSubAccount(): BelongsTo
    {
        return $this->belongsTo(SubAccount::class, 'new_sub_account_id');
    }

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