<?php

declare(strict_types=1);

namespace Kylin\Library\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Kylin\Core\Models\BaseModel;

/**
 * 借阅记录模型
 *
 * 依据《公共图书馆法》第四十三条：
 * 读者应按时归还图书，逾期需缴纳罚款。
 *
 * @property int    $id
 * @property int    $reader_id      读者 ID
 * @property int    $book_id        图书 ID
 * @property string $borrowed_at    借阅日期
 * @property string $due_at         应还日期
 * @property string $returned_at    实际归还日期
 * @property int    $renew_count    续借次数
 * @property float  $fine_amount    罚款金额
 * @property string $status         状态
 */
class BorrowRecord extends BaseModel
{
    protected $table = 'kylin_library_borrow_records';

    protected $fillable = [
        'reader_id', 'book_id',
        'borrowed_at', 'due_at', 'returned_at',
        'renew_count', 'fine_amount', 'status',
    ];

    protected $casts = [
        'borrowed_at' => 'datetime',
        'due_at' => 'datetime',
        'returned_at' => 'datetime',
        'renew_count' => 'integer',
        'fine_amount' => 'float',
    ];

    /**
     * 所属读者
     */
    public function reader(): BelongsTo
    {
        return $this->belongsTo(Reader::class, 'reader_id');
    }

    /**
     * 所属图书
     */
    public function book(): BelongsTo
    {
        return $this->belongsTo(Book::class, 'book_id');
    }
}