<?php

namespace App\Models;

use App\Traits\Auditable;

use Illuminate\Database\Eloquent\Model;

/**
 * @property string $id
 * @property string $especiality_id
 * @property string $user_profile_id
 * @property string $payment_methods_id
 * @property string $currencies_id
 * @property float $amount
 * @property string $reference
 * @property string $description
 * @property string $payer
 * @property string $address_payer
 * @property string $created_at
 * @property string $updated_at
 * @property Currency $currency
 * @property Especiality $especiality
 * @property PaymentMethod $paymentMethod
 * @property UserProfile $userProfile
 */
class Payments extends Model
{
    use Auditable;
    /**
     * The "type" of the auto-incrementing ID.
     * 
     * @var string
     */
    protected $keyType = 'string';

    /**
     * Indicates if the IDs are auto-incrementing.
     * 
     * @var bool
     */
    public $incrementing = false;

    protected $table = 'payments';  

    /**
     * @var array
     */
    protected $fillable = ['id','especiality_id', 'active', 'users_id', 'payment_methods_id', 'currencies_id', 'amount', 'is_electronic', 'user_reference', 'reference', 'description', 'payer', 'address_payer', 'created_at', 'updated_at', 'appointment_id', 'parent_id'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function currency()
    {
        return $this->belongsTo('App\Models\Currencies', 'currencies_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function especiality()
    {
        return $this->belongsTo('App\Models\Especiality');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function paymentMethod()
    {
        return $this->belongsTo('App\Models\PaymentMethods', 'payment_methods_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('App\Models\User', 'users_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function userProfile()
    {
        return $this->belongsTo('App\Models\UserProfile', 'users_id', 'id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function appointment()
    {
        return $this->belongsTo('App\Models\Appointments', 'appointment_id', 'id');
    }

    /**
     * Marcar una especialidad como inactiva
     */
    public function inactive(){
        $this->active = 0;
        return $this->save();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function medicalServices()
    {
        return $this->belongsToMany(MedicalServiceSnapshot::class, 'payment_medical_service', 'payment_id', 'medical_service_id')
            ->withTimestamps();
    }

    /**
     * Obtiene el monto total pagado (suma de precios de servicios médicos)
     */
    public function getPaidAmountAttribute()
    {
        return $this->medicalServices()->sum('cost');
    }

    /**
     * Obtiene el pago padre
     */
    public function parent()
    {
        return $this->belongsTo(self::class, 'parent_id');
    }

    /**
     * Obtiene los pagos hijos
     */
    public function children()
    {
        return $this->hasMany(self::class, 'parent_id');
    }
}
