<?php

namespace App\Models;

use App\Enums\StatusManagement;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

class ManagementSubmission extends Model
{
    use HasFactory;

    protected $table = 'management_submissions';

    protected $fillable = [
        'user_id',
        'status',
        'visit_dealer',
        'visit_retailer',
        'visit_farmer',
        'by',
        'note_by',
        'by_at',
    ];

    protected function casts(): array
    {
        return [
            'user_id' => 'integer',
            'visit_dealer' => 'integer',
            'visit_retailer' => 'integer',
            'visit_farmer' => 'integer',
            'by' => 'integer',
            'note_by' => 'string',
            'by_at' => 'datetime',
            'status' => StatusManagement::class,
            'created_at' => 'datetime',
            'updated_at' => 'datetime',
        ];
    }

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

    protected function createdAtWib(): Attribute
    {
        return Attribute::make(
            get: fn($value, $attributes) => Carbon::parse($attributes['created_at'])
                ->timezone('Asia/Jakarta')
                ->format('d/m/Y H:i:s'),
        );
    }

    protected function createdAtDate(): Attribute
    {
        return Attribute::make(
            get: fn($value, $attributes) => Carbon::parse($attributes['created_at'])
                ->timezone('Asia/Jakarta')
                ->format('d/m/Y'),
        );
    }

    protected function createdAtTime(): Attribute
    {
        return Attribute::make(
            get: fn($value, $attributes) => Carbon::parse($attributes['created_at'])
                ->timezone('Asia/Jakarta')
                ->format('H:i:s'),
        );
    }

    protected function byAtDate(): Attribute
    {
        return Attribute::make(
            get: fn($value, $attributes) => Carbon::parse($attributes['created_at'])
                ->timezone('Asia/Jakarta')
                ->format('d/m/Y'),
        );
    }

    protected function byAtTime(): Attribute
    {
        return Attribute::make(
            get: fn($value, $attributes) => Carbon::parse($attributes['created_at'])
                ->timezone('Asia/Jakarta')
                ->format('H:i:s'),
        );
    }
}

