<?php

namespace App\Models\System;

use App\Models\Machine\Machine;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Permission\Models\Role;

class Company extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'name',
        'code',
        'original_type',
        'current_type',
        'tax_id',
        'contact_name',
        'contact_phone',
        'contact_email',
        'status',
        'start_date',
        'end_date',
        'warranty_start_date',
        'warranty_end_date',
        'software_start_date',
        'software_end_date',
        'note',
        'settings',
    ];

    protected $casts = [
        'start_date' => 'date:Y-m-d',
        'end_date' => 'date:Y-m-d',
        'warranty_start_date' => 'date:Y-m-d',
        'warranty_end_date' => 'date:Y-m-d',
        'software_start_date' => 'date:Y-m-d',
        'software_end_date' => 'date:Y-m-d',
        'status' => 'integer',
        'settings' => 'array',
    ];

    /**
     * Get the contract history for the company.
     */
    public function contracts(): HasMany
    {
        return $this->hasMany(CompanyContract::class)->latest();
    }

    /**
     * Get the users for the company.
     */
    public function users(): HasMany
    {
        return $this->hasMany(User::class);
    }

    /**
     * Get the machines for the company.
     */
    public function machines(): HasMany
    {
        return $this->hasMany(Machine::class);
    }
}
