<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\DesignationMaster;
use App\Models\DeptMaster;


class ActionMaster extends Model
{
    use SoftDeletes;

    protected $table = 'action_master';

    protected $primaryKey = 'Act_Id';


    protected $fillable = [
        'Dpt_id',
        'St_Name',
        'Previous_Status',
        'stage',
        'Viewers',
        'Authorities',
        'Act_Remarks',
        'Is_Forward',
        'Is_Bank_Select',
        'Is_Acc_Vou_No_Gen'
    ];


    protected $casts = [
        'Authorities' => 'array',
        'Viewers' => 'array'

    ];

    protected $appends = [
        'authorities_details',
        'viewers_details'
    ];

    public function department()
    {
        return $this->belongsTo(Department::class, 'Dpt_id', 'Dpt_id');
    }


    public function previousStatus()
    {
        return $this->belongsTo(ActionMaster::class, 'Previous_Status', 'Act_Id');
    }
    public function getAuthoritiesDetailsAttribute()
    {
        if (!$this->Authorities) {
            return [];
        }
        $ids = $this->Authorities;

        if (is_string($ids)) {
            $ids = json_decode($ids, true);
        }
        if (is_string($ids)) {
            $ids = json_decode($ids, true);
        }
        if (!is_array($ids)) {
            return [];
        }
        return DesignationMaster::whereIn('Dsg_id', $ids)->get();
    }


    public function getViewersDetailsAttribute()
    {
        if (!$this->Viewers) {
            return [];
        }
        $ids = $this->Viewers;

        if (is_string($ids)) {
            $ids = json_decode($ids, true);
        }

        if (is_string($ids)) {
            $ids = json_decode($ids, true);
        }

        if (!is_array($ids)) {
            return [];
        }

        return DesignationMaster::whereIn('Dsg_id', $ids)->get();
    }
}
