<?php

namespace App\Models;

use App\Models\ValidationModel;
use App\Models\User;

class FieldChange extends ValidationModel
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'field_changes';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['related_id', 'related_type', 'field_name', 'old_value', 'new_value', 'changed_by'];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['field_label'];

    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'related_id'   => 'required',
        'related_type' => 'required',
        'field_name'   => 'required',
    ];

    /**
     * Insert the validation checks on the model.
     * The model only gets created if the validation checks pass.
     *
     * @var void
     */
    public static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            if (!$model->changed_by) {
                $model->changed_by = getAuthUserId();
            }
        });
    }

    /**
     * Returns all the field changes for tasks.
     */
    public function scopeTasks($query)
    {
        return $query->where('related_type', 'task');
    }

    /**
     * Returns the user who made the change.
     *
     * @return \User
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'changed_by');
    }

    /**
     * Get the human-readable field label.
     *
     * @return string
     */
    public function getFieldLabelAttribute()
    {
        $labels = [
            'title' => 'Title',
            'problem' => 'Problem Description',
            'assigned_to' => 'Assigned To',
            'severity_id' => 'Severity',
            'type' => 'Task Type',
            'subtype_id' => 'Subtype',
            'standard_price_id' => 'Repair Type',
            'return_id' => 'Return / Old job id',
            'labour' => 'Labour Cost',
            'needs_delivery' => 'Needs Delivery',
            'delivered_by' => 'Delivered By',
            'delivered' => 'Delivered',
            'needs_pickup' => 'Needs Pickup',
            'pickup_by' => 'Pickup By',
            'pickup_schedule' => 'Pickup Schedule',
            'picked_up_at' => 'Picked Up',
            'pin' => 'PIN',
            'shelf' => 'Shelf Location',
            'shelf_type' => 'Shelf Type',
            'prebook' => 'Prebook',
            'prebook_date' => 'Prebook Date',
            'onsite_start' => 'Onsite Start',
            'onsite_end' => 'Onsite End',
            'time_spent' => 'Time Spent',
            'attended_time' => 'Attended Time',
            'unattended_time' => 'Unattended Time',
            'alarm_date' => 'Alarm Date',
            'due_date' => 'Due Date',
            'passed_qa' => 'Passed QA',
            'satisfied_client' => 'Client Satisfied',
            'needs_calling' => 'Needs Calling',
            'cancelled_reason' => 'Cancellation Reason',
            'antivirus_state' => 'Antivirus State',
            'no_price_reason' => 'No Price Reason',
            'nopassword_reason' => 'No Password Reason',
            'direct_dial' => 'Direct Dial',
        ];

        return $labels[$this->field_name] ?? $this->field_name;
    }
}
