<?php

namespace App\Models\OLDLMS;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Appointment extends Model
{
    use HasFactory, SoftDeletes;

    const CREATED_AT = 'dCreateOn';
    const UPDATED_AT = 'dUpdateOn';
    const DELETED_AT = 'dDeleteOn';

    public $sStatusNames = [
        0 => 'Menunggu Pembayaran',
        1 => 'Pembayaran Terkonfirmasi', // Pembayaran Diterima
        2 => 'Ditolak',
        3 => 'Dibatalkan', // Canceled
        4 => 'Expired',
    ];

    public $sPaymentMethodName = [
        1 => 'Pribadi',
        2 => 'On-Site Payment',
        3 => 'OVO',
        4 => 'Asuransi',
        5 => 'Voucher',
        6 => 'ASO'
    ];

    public $sPaymentStatusName = [
        'settlement' => 'Diterima',
        'expired' => 'Kadaluarsa',
        'capture' => 'Captured',
        'deny' => 'Ditolak',
        'pending' => 'Menunggu Pembayaran',
        'cancel' => 'Dibatalkan',
        'refund' => 'Dikembalikan',
        'expire' => 'Kadaluarsa',
        'cod' => 'COD',
        'FAILED' => 'Gagal',
        'COMPLETED' => 'Complete',
    ];

    public $nIDJenisBookingNames = [
        1 => 'Rawat Jalan',
        2 => 'Telekonsultasi',
        3 => 'Chat Sekarang'
    ];

    protected $connection = 'oldlms';

    protected $table = 'tx_appointment';

    protected $primaryKey = 'nID';

    public $incrementing = false;

    protected $keyType = 'string';

    protected $fillable = [
        'nID',
        'nIDDokter',
        'nIDUser',
        'sStatus',
        'dCreateOn',
        'dUpdateOn',
        'dDeleteOn',
    ];

    protected $appends = [
        'status_name',
        'payment_method',
        'type',
        'payment_status'
    ];

    protected function statusName(): Attribute
    {
        return Attribute::make(
            get: function ($value) {
                return $this->sStatusNames[$this->sStatus] ?? '-';
            },
        );
    }


    protected function paymentStatus(): Attribute
    {
        return Attribute::make(
            get: function ($value) {
                return $this->sPaymentStatusName[$this->sPaymentStatus] ?? $this->sPaymentStatus;
            },
        );
    }

    protected function paymentMethod(): Attribute
    {
        return Attribute::make(
            get: function ($value) {
                return $this->sPaymentMethodName[$this->sPaymentMethod] ?? '-';
            },
        );
    }

    protected function type(): Attribute
    {
        return Attribute::make(
            get: function ($value) {
                return $this->nIDJenisBookingNames[$this->nIDJenisBooking] ?? '-';
            }
        );
    }

    protected function shareKonsultasi(): Attribute
    {
        return Attribute::make(
            get: function () {
                $consulPrice = ((float) $this->detail->sPaymentDetails['gross_amount'] ?? 0) - $this->nAdminFee;
                if ($this->nIDJenisBooking == 3) { // Telekonsultasi Sekarang
                    return $consulPrice * (100 - $this->healthCare->commission->nCommissionATC) / 100;
                } else if ($this->nIDJenisBooking == 2) { // Telekonsultasi
                    return $consulPrice * (100 - $this->healthCare->commission->nCommissionTC) / 100;
                } else {  // Walk In
                    return $consulPrice * (100 - $this->healthCare->commission->nCommission) / 100;
                }
            }
        );
    }

    protected function shareKomisi(): Attribute
    {
        return Attribute::make(
            get: function () {
                $consulPrice = ((float) $this->detail->sPaymentDetails['gross_amount'] ?? 0) - $this->nAdminFee;
                if ($this->nIDJenisBooking == 3) { // Telekonsultasi Sekarang
                    return $consulPrice * ($this->healthCare->commission->nCommissionATC) / 100;
                } else if ($this->nIDJenisBooking == 2) { // Telekonsultasi
                    return $consulPrice * ($this->healthCare->commission->nCommissionTC) / 100;
                } else {  // Walk In
                    return $consulPrice * ($this->healthCare->commission->nCommission) / 100;
                }
            }
        );
    }

    public function appointmentDetail()
    {
        return $this->hasOne(AppointmentDetail::class, 'nIDAppointment', 'nID');
    }

    public function detail()
    {
        return $this->hasOne(AppointmentDetail::class, 'nIDAppointment', 'nID');
    }

    public function doctor()
    {
        return $this->belongsTo(Dokter::class, 'nIDDokter', 'nID');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'nIDUser', 'nID');
    }

    public function healthCare()
    {
        return $this->belongsTo(Healthcare::class, 'nIDHealthCare', 'nID');
    }

    public function consulPrice(){
        return $this->hasOne(JadwalDokter::class, 'nIDDokter', 'nIDDokter');
    }
}
