<?php

namespace App\Models\Transaction;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Traits\TenantScoped;
use App\Models\Machine\Machine;
use App\Models\Member\Member;

class Order extends Model
{
    use HasFactory, SoftDeletes, TenantScoped;

    // 支付狀態
    public const PAYMENT_STATUS_FAILED = 0;
    public const PAYMENT_STATUS_SUCCESS = 1;

    protected $fillable = [
        'company_id',
        'flow_id',
        'order_no',
        'machine_id',
        'member_id',
        'payment_type',
        'total_amount',
        'discount_amount',
        'pay_amount',
        'change_amount',
        'points_used',
        'original_amount',
        'payment_status',
        'payment_request',
        'payment_response',
        'payment_at',
        'member_barcode',
        'code_id',
        'invoice_info',
        'machine_time',
        'status',
        'metadata',
    ];

    protected $casts = [
        'total_amount' => 'decimal:2',
        'discount_amount' => 'decimal:2',
        'pay_amount' => 'decimal:2',
        'change_amount' => 'decimal:2',
        'original_amount' => 'decimal:2',
        'payment_at' => 'datetime',
        'machine_time' => 'datetime',
        'metadata' => 'array',
        'payment_type' => 'integer',
    ];

    /**
     * 取得所有支付類型的對照標籤
     */
    public static function getPaymentTypeLabels(): array
    {
        return [
            1 => __('Credit Card'),
            2 => __('E-Ticket (EasyCard/iPass)'),
            3 => __('QR Code Payment'),
            4 => __('Bill Acceptor'),
            5 => __('Pass Code'), // 使用者要求由「通關密碼」改為「通行碼」
            6 => __('Pickup Code'),
            7 => __('Welcome Gift'),
            8 => __('Questionnaire'),
            9 => __('Coin Acceptor'),
            21 => __('Offline + 1'),
            22 => __('Offline + 2'),
            23 => __('Offline + 3'),
            24 => __('Offline + 4'),
            25 => __('Offline + 9'),
            30 => 'LINE Pay',
            31 => __('JKO Pay'),
            32 => __('Easy Wallet'),
            33 => 'Pi Pay',
            34 => 'PlusPay',
            40 => __('Member Verify Pickup'),
            41 => __('Staff Card'),
            50 => __('Offline + LINE Pay'),
            51 => __('Offline + JKO Pay'),
            52 => __('Offline + Easy Wallet'),
            53 => __('Offline + Pi Pay'),
            54 => __('Offline + PlusPay'),
            60 => __('Points/Voucher'),
            61 => __('Member + 1'),
            62 => __('Member + 2'),
            63 => __('Member + 3'),
            64 => __('Member + 4'),
            65 => __('Member + 5'),
            66 => __('Member + 6'),
            67 => __('Member + 7'),
            68 => __('Member + 8'),
            69 => __('Member + 9'),
            90 => __('Member + LINE Pay'),
            91 => __('Member + JKO Pay'),
            92 => __('Member + Easy Wallet'),
            93 => __('Member + Pi Pay'),
            94 => __('Member + PlusPay'),
        ];
    }

    /**
     * 取得目前的支付類型標籤 (Accessor)
     */
    public function getPaymentTypeLabelAttribute(): string
    {
        return self::getPaymentTypeLabels()[$this->payment_type] ?? __('Unknown');
    }

    public function machine()
    {
        return $this->belongsTo(Machine::class);
    }

    public function member()
    {
        return $this->belongsTo(Member::class);
    }

    public function items()
    {
        return $this->hasMany(OrderItem::class);
    }

    public function invoice()
    {
        return $this->hasOne(Invoice::class);
    }

    public function dispenseRecords()
    {
        return $this->hasMany(DispenseRecord::class);
    }
}
