<?php

namespace App\Modules\Bookings;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use App\Modules\Bookings\Repair;
use App\Modules\Bookings\ModelNumber;
use App\Modules\Clients\Client;
use App\Modules\Clients\Customer;
use App\Traits\SetCreatorAndUpdatorTrait;

class Device extends Model
{
    use \Venturecraft\Revisionable\RevisionableTrait;
    use SetCreatorAndUpdatorTrait;
    use SoftDeletes;

    /**
     * The attributes that do not want to keef revision of.
     *
     * @var array
     */
    protected $KeepRevisionOf = ['case', 'psu', 'visual_inspection', 'other_items', 'has_antivirus', 'antivirus', 'antivirus_expiration_date', 'is_secure', 'num_viruses', 'ram_model', 'ram_speed', 'ram_status', 'ram_size', 'recommended_ram_size', 'hdd_model', 'hdd_status', 'hdd_size'];

    /**
     * The attributes that do not want to keef revision of.
     *
     * @var array
     */
    protected $revisionFormattedFields = [
    'ram_speed' => 'isEmpty:|%sGHz',
    'ram_size'  => 'isEmpty:|%sGB',
    'hdd_size'  => 'isEmpty:|%sGB',
    ];

    /**
     * The attributes that do not want to keef revision of.
     *
     * @var array
     */
    protected $revisionFormattedFieldNames = [
    'case'                      => 'Has Case',
    'psu'                       => 'Has PSU',
    'visual_inspection'         => 'Visual Inspection',
    'other_items'               => 'Other items',
    'has_antivirus'             => 'Has Antivirus',
    'antivirus'                 => 'Antivirus Program',
    'antivirus_expiration_date' => 'Antivirus Expiration Date',
    'is_secure'                 => 'Is Secure',
    'num_viruses'               => 'Number of Viruses',
    'ram_model'                 => 'RAM Model',
    'ram_speed'                 => 'RAM Speed',
    'ram_status'                => 'RAM Status',
    'ram_size'                  => 'RAM Size',
    'recommended_ram_size'      => 'Recommended RAM Size',
    'hdd_model'                 => 'HDD Model',
    'hdd_status'                => 'HDD Status',
    'hdd_size'                  => 'HDD Size',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['client_id', 'client_type', 'type', 'model_number_id', 'case', 'psu', 'visual_inspection', 'other_items', 'has_antivirus', 'antivirus', 'antivirus_expiration_date', 'is_secure', 'num_viruses', 'ram_model', 'ram_speed', 'ram_status', 'ram_size', 'recommended_ram_size', 'hdd_model', 'hdd_status', 'hdd_size', 'created_by', 'updated_by', 'serial_number'];

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

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['name', 'creator_name', 'updater_name', 'icon'];

    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'client_id'          => 'required',
        'client_type'        => 'required',
        'type'               => 'required',
        'model_number_id'    => 'required',
        'serial_number'      => 'unique:devices,serial_number,{id},id',
    ];

    /**
     * Boot the model and set up the morphMap for polymorphic relationships.
     *
     * @return void
     */
    public static function boot()
    {
        parent::boot();
        
        // Set up morphMap for polymorphic relationships
        \Illuminate\Database\Eloquent\Relations\Relation::morphMap([
            'client' => Client::class,
            'customer' => Customer::class,
        ]);
    }

    //  /**
    //  * Returns the device name build with the manufacturer name and the model number name
    //      //  * @return string
    //  */
    public function getNameAttribute()
    {
        $model = $this->model_number()->first();

        return $model ? $model->manufacturer->name.' '.$model->model_number : '';
    }

    /**
     * Returns all the repairs that have this device.
     *
     * @return \App\Modules\Bookings\Repair
     */
    public function repairs()
    {
        return $this->belongsToMany(Repair::class, 'task_devices', 'device_id', 'task_id');
    }

    /**
     * The model number of this repair.
     *
     * @return \App\Modules\Bookings\ModelNumber
     */
    public function model_number()
    {
        return $this->belongsTo(ModelNumber::class, 'model_number_id');
    }

    /**
     * Get the client (polymorphic relationship).
     * This can be either a Client or Customer model.
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function client()
    {
        return $this->morphTo('client', 'client_type', 'client_id');
    }

    /**
     *Returns the antiviruses types.
     *
     *@return array
     */
    public static function antiviruses()
    {
        return ['ESET', 'AVG','Avira', 'Avast','Kaspersky','Panda','Sophos','other'];
    }

    /**
     *Returns the antiviruses types.
     *
     *@return array
     */
    public static function RAMModels()
    {
        return ['DDR2','DDR3','DDR4','other'];
    }

    /**
     *Returns the antiviruses types.
     *
     *@return array
     */
    public static function HDDModels()
    {
        return ['SATA','ATA','SSD','Hybrid','mSATA','SCSI'];
    }

    /**
     * Get the FontAwesome icon class for this device type
     *
     * @return string
     */
    public function getIconAttribute(): string
    {
        // Define icon mapping for known device types
        $iconMap = [
            'laptop' => 'fa fa-laptop',
            'pc' => 'fa fa-desktop',
            'mac' => 'fa fa-apple',
            'tablet' => 'fa fa-tablet',
            'hdd' => 'fa fa-hdd-o',
            'amplifier' => 'fa fa-volume-up',
            'speaker_amp' => 'fa fa-volume-up',
            'guitar_amp' => 'fa fa-music',
            'keyboard' => 'fa fa-keyboard-o',
            'dj_controller' => 'fa fa-sliders',
            'dj_mixer' => 'fa fa-sliders',
            'mixing_desk' => 'fa fa-sliders',
            'microphone' => 'fa fa-microphone',
            'speaker' => 'fa fa-volume-up',
            'monitor' => 'fa fa-tv',
            'printer' => 'fa fa-print',
            'camera' => 'fa fa-camera',
            'phone' => 'fa fa-phone',
            'game_console' => 'fa fa-gamepad',
        ];

        // Return specific icon or fallback to generic device icon
        return $iconMap[$this->type] ?? 'fa fa-cog';
    }

    /**
     * Get all supported device types with their icons
     *
     * @return array
     */
    public static function getDeviceTypes(): array
    {
        return [
            'laptop' => ['name' => 'Laptop', 'icon' => 'fa fa-laptop'],
            'pc' => ['name' => 'Desktop PC', 'icon' => 'fa fa-desktop'],
            'mac' => ['name' => 'Mac', 'icon' => 'fa fa-apple'],
            'tablet' => ['name' => 'Tablet', 'icon' => 'fa fa-tablet'],
            'hdd' => ['name' => 'Hard Drive', 'icon' => 'fa fa-hdd-o'],
            'amplifier' => ['name' => 'Amplifier', 'icon' => 'fa fa-volume-up'],
            'speaker_amp' => ['name' => 'Speaker Amplifier', 'icon' => 'fa fa-volume-up'],
            'guitar_amp' => ['name' => 'Guitar Amplifier', 'icon' => 'fa fa-music'],
            'keyboard' => ['name' => 'Keyboard', 'icon' => 'fa fa-keyboard-o'],
            'dj_controller' => ['name' => 'DJ Controller', 'icon' => 'fa fa-sliders'],
            'dj_mixer' => ['name' => 'DJ Mixer', 'icon' => 'fa fa-sliders'],
            'mixing_desk' => ['name' => 'Mixing Desk', 'icon' => 'fa fa-sliders'],
            'microphone' => ['name' => 'Microphone', 'icon' => 'fa fa-microphone'],
            'speaker' => ['name' => 'Speaker', 'icon' => 'fa fa-volume-up'],
            'monitor' => ['name' => 'Monitor', 'icon' => 'fa fa-tv'],
            'printer' => ['name' => 'Printer', 'icon' => 'fa fa-print'],
            'camera' => ['name' => 'Camera', 'icon' => 'fa fa-camera'],
            'phone' => ['name' => 'Phone', 'icon' => 'fa fa-phone'],
            'game_console' => ['name' => 'Game Console', 'icon' => 'fa fa-gamepad'],
        ];
    }
}
