<?php

declare(strict_types=1);

namespace App\Modules\Invitation;

use App\Core\Database;

/**
 * GiftModel — Database operations for Wedding Gift.
 *
 * @package Inveetaire
 */
class GiftModel
{
    /**
     * Get gift settings by invitation ID.
     */
    public function getSettingsByInvitationId(int $invitationId): ?array
    {
        $db = Database::getConnection();
        $stmt = $db->prepare('
            SELECT id, invitation_id, is_enabled, title, subtitle
            FROM wedding_gift_settings
            WHERE invitation_id = :invitation_id
            LIMIT 1
        ');
        $stmt->execute(['invitation_id' => $invitationId]);
        $row = $stmt->fetch();
        return $row ?: null;
    }

    /**
     * Get gift accounts by setting ID.
     */
    public function getAccountsBySettingId(int $settingId): array
    {
        $db = Database::getConnection();
        $stmt = $db->prepare('
            SELECT id, gift_setting_id, provider_name, account_number, account_holder, account_type, is_active, sort_order
            FROM wedding_gift_accounts
            WHERE gift_setting_id = :setting_id
            ORDER BY sort_order ASC, id ASC
        ');
        $stmt->execute(['setting_id' => $settingId]);
        return $stmt->fetchAll();
    }

    /**
     * Update or create gift settings.
     */
    public function saveSettings(int $invitationId, bool $isEnabled, string $title, ?string $subtitle): void
    {
        $db = Database::getConnection();
        
        $existing = $this->getSettingsByInvitationId($invitationId);
        
        if ($existing) {
            $stmt = $db->prepare('
                UPDATE wedding_gift_settings 
                SET is_enabled = :is_enabled, title = :title, subtitle = :subtitle
                WHERE invitation_id = :invitation_id
            ');
            $stmt->execute([
                'is_enabled' => $isEnabled ? 1 : 0,
                'title' => $title,
                'subtitle' => $subtitle,
                'invitation_id' => $invitationId,
            ]);
        } else {
            $stmt = $db->prepare('
                INSERT INTO wedding_gift_settings (invitation_id, is_enabled, title, subtitle)
                VALUES (:invitation_id, :is_enabled, :title, :subtitle)
            ');
            $stmt->execute([
                'invitation_id' => $invitationId,
                'is_enabled' => $isEnabled ? 1 : 0,
                'title' => $title,
                'subtitle' => $subtitle,
            ]);
        }
    }

    /**
     * Save an account.
     */
    public function saveAccount(int $settingId, int $accountId, string $providerName, string $accountNumber, string $accountHolder, bool $isActive, int $sortOrder): void
    {
        $db = Database::getConnection();
        
        if ($accountId > 0) {
            $stmt = $db->prepare('
                UPDATE wedding_gift_accounts 
                SET provider_name = :provider_name, account_number = :account_number, account_holder = :account_holder, is_active = :is_active, sort_order = :sort_order
                WHERE id = :id AND gift_setting_id = :setting_id
            ');
            $stmt->execute([
                'id' => $accountId,
                'setting_id' => $settingId,
                'provider_name' => $providerName,
                'account_number' => $accountNumber,
                'account_holder' => $accountHolder,
                'is_active' => $isActive ? 1 : 0,
                'sort_order' => $sortOrder
            ]);
        } else {
            $stmt = $db->prepare('
                INSERT INTO wedding_gift_accounts (gift_setting_id, provider_name, account_number, account_holder, is_active, sort_order)
                VALUES (:setting_id, :provider_name, :account_number, :account_holder, :is_active, :sort_order)
            ');
            $stmt->execute([
                'setting_id' => $settingId,
                'provider_name' => $providerName,
                'account_number' => $accountNumber,
                'account_holder' => $accountHolder,
                'is_active' => $isActive ? 1 : 0,
                'sort_order' => $sortOrder
            ]);
        }
    }

    /**
     * Delete an account.
     */
    public function deleteAccount(int $settingId, int $accountId): void
    {
        $db = Database::getConnection();
        $stmt = $db->prepare('
            DELETE FROM wedding_gift_accounts 
            WHERE id = :id AND gift_setting_id = :setting_id
        ');
        $stmt->execute([
            'id' => $accountId,
            'setting_id' => $settingId
        ]);
    }
}
