<?php

namespace App\Models;

use App\Models\ValidationModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Status extends ValidationModel
{
    const TASKS_BOOKED_IN = 1;
    const ORDERS_NOT_ATTEMPTED = 2;
    const TASKS_WORK_IN_PROGRESS = 4;
    const TASKS_CUSTOMER_NEEDS_TO_BE_CALLED = 5;
    const TASKS_CUSTOMER_WILL_GET_BACK_TO_US = 6;
    const TASKS_WAITING_FOR_PARTS = 7;
    const TASKS_CANCELLED = 8;
    const TASKS_QUALITY_CONTROL = 9;
    const TASKS_WORK_COMPLETED = 10;
    const TASKS_COLLECTED = 11;
    const TASKS_INVOICED = 12;
    const TASKS_DELIVERED = 13;
    const ORDERS_OUT_OF_STOCK = 14;
    const ORDERS_ORDERED = 15;
    const ORDERS_DELIVERED = 16;
    const TASKS_DUPLICATE = 26;
    const ORDERS_CANCELLED = 29;
    const ORDERS_COLLECTED = 30;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'statuses';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'for', 'completed', 'private', 'exclude_notifications'];

    /**
     * Returns a list of items that the status can be applied to.
     *
     * @return array
     */
    public static function getFor()
    {
        return [
            'repairs'        => 'Repairs',
            'orders'         => 'Orders',
            'invoicing'      => 'Invoices',
            'rma'            => 'RMA',
            'onsite'         => 'Onsite',
            'sale_lead'      => 'Sale Leads',
            'purchase_order' => 'Purchas Order',
        ];
    }

    /**
     * Returns all the statuses for repairs.
     */
    public function scopeRepairs($query)
    {
        return $query->where('for', 'repairs');
    }

    /**
     * Returns all the statuses for remote support.
     */
    public function scopeSupport($query)
    {
        return $query->where('for', 'support');
    }

    /**
     * Returns all the statuses for orders.
     */
    public function scopeOrders($query)
    {
        return $query->where('for', 'orders');
    }

    /**
     * Returns all the statuses for rma.
     */
    public function scopeRma($query)
    {
        return $query->where('for', 'rma');
    }

    /**
     * Returns all the statuses for rma.
     */
    public function scopeProject($query)
    {
        return $query->where('for', 'project');
    }

    /**
     * Returns all the statuses for sale leads.
     */
    public function scopeSaleLeads($query)
    {
        return $query->where('for', 'sale_lead');
    }

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