<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Company extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'name',
        'commercial_name',
        'status',
        'address',
        'postal_code',
        'city',
        'country',
        'phone',
        'email',
        'cif',
        'logo'
    ];

    protected $casts = [
        'created_at' => 'datetime:d/m/Y H:i',
        'updated_at' => 'datetime:d/m/Y H:i',
    ];

    public function getStatusColorAttribute()
    {
        return [
            'active' => 'bg-green-100 text-green-800',
            'closed' => 'bg-red-100 text-red-800',
        ][$this->status] ?? 'bg-gray-100 text-gray-800';
    }

    public function getStatusTextAttribute()
    {
        return [
            'active' => 'Activo',
            'closed' => 'Cerrado',
        ][$this->status] ?? 'Desconocido';
    }

    public function contacts(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'company_contacts')
                    ->withPivot('position')
                    ->withTimestamps();
    }

    public function users()
    {
        return $this->hasMany(User::class);
    }
    
    public function projects()
    {
        return $this->hasMany(Project::class);
    }
}
