<?php

namespace App\Models\Barobill;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class CardTransactionHide extends Model
{
    protected $table = 'barobill_card_transaction_hides';

    protected $fillable = [
        'tenant_id',
        'original_unique_key',
        'card_num',
        'use_date',
        'approval_num',
        'original_amount',
        'merchant_name',
        'hidden_by',
    ];

    /**
     * 기간 내 숨김된 키 목록 조회
     */
    public static function getHiddenKeys(int $tenantId, string $startDate, string $endDate): Collection
    {
        return static::where('tenant_id', $tenantId)
            ->where('use_date', '>=', $startDate)
            ->where('use_date', '<=', $endDate)
            ->pluck('original_unique_key');
    }

    /**
     * 거래 숨김 처리
     */
    public static function hideTransaction(int $tenantId, string $uniqueKey, array $originalData, ?int $userId = null): static
    {
        return static::create([
            'tenant_id' => $tenantId,
            'original_unique_key' => $uniqueKey,
            'card_num' => $originalData['cardNum'] ?? '',
            'use_date' => $originalData['useDate'] ?? '',
            'approval_num' => $originalData['approvalNum'] ?? '',
            'original_amount' => floatval($originalData['approvalAmount'] ?? 0),
            'merchant_name' => $originalData['merchantName'] ?? '',
            'hidden_by' => $userId,
        ]);
    }

    /**
     * 거래 복원 (숨김 해제)
     */
    public static function restoreTransaction(int $tenantId, string $uniqueKey): int
    {
        return static::where('tenant_id', $tenantId)
            ->where('original_unique_key', $uniqueKey)
            ->delete();
    }
}
