<?php

namespace App\Models;

use App\Traits\Auditable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ClinicIdentity extends Model
{
    use HasFactory;
    use Auditable;

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

    /**
     * @var array
     */
    protected $fillable = [
        'name',
        'address',
        'phone',
        'email',
        'tax_id',
        'logo_path',
        'website',
        'description',
        'specialty',
        'social_media_links',
        'working_hours',
        'emergency_contact',
        'is_active'
    ];

    /**
     * Obtiene la identidad activa del consultorio
     * 
     * @return ClinicIdentity|null
     */
    public static function getActive()
    {
        return self::where('is_active', true)->first();
    }

    /**
     * Desactiva todas las identidades excepto la actual
     * 
     * @return void
     */
    public function setAsActive()
    {
        // Desactivar todas las identidades
        self::where('id', '!=', $this->id)->update(['is_active' => false]);
        
        // Activar esta identidad
        $this->is_active = true;
        $this->save();
    }

     /**
     * Obtiene la identidad activa del consultorio
     * 
     * @return ClinicIdentity|null
     */
    public static function getActiveIdentity()
    {
        return ClinicIdentity::getActive();
    }
} 