<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Manager extends Model
{
    protected $table = 'manager';
    protected $primaryKey = 'manager_id';
    public $incrementing = true;
    protected $keyType = 'int';

    // timestamps は created_at / updated_at があるのでデフォルト true のまま

    protected $fillable = [
        'manager_name',
        'manager_type',
        'manager_parkid',
        'manager_device1',
        'manager_device2',
        'manager_mail',
        'manager_tel',
        'manager_alert1',
        'manager_alert2',
        'manager_quit_flag',
        'manager_quitday',
        'operator_id',
    ];

    protected $casts = [
        'manager_parkid'     => 'integer',
        'manager_device1'    => 'integer',
        'manager_device2'    => 'integer',
        'manager_alert1'     => 'boolean',
        'manager_alert2'     => 'boolean',
        'manager_quit_flag'  => 'boolean',
        'manager_quitday'    => 'date',
        'operator_id'        => 'integer',
    ];

    // --- リレーション（テーブル名は既存に合わせて調整してください）
    public function park()    { return $this->belongsTo(Park::class,   'manager_parkid',  'park_id'); }
    public function device1() { return $this->belongsTo(Device::class, 'manager_device1', 'device_id'); }
    public function device2() { return $this->belongsTo(Device::class, 'manager_device2', 'device_id'); }

    // Blade 互換のヘルパ（list.blade.php で getXxx() を呼んでいるため）
    public function getPark()    { return $this->park; }
    public function getDevice1() { return $this->device1; }
    public function getDevice2() { return $this->device2; }

    public function getManagerQuitFlagDisplay()
    {
        return $this->manager_quit_flag ? '退職' : '在職';
    }
    public function getManagerQuitFlagDisplayAttribute()
    {
        return $this->getManagerQuitFlagDisplay();
    }
}
