<?php

declare(strict_types=1);

namespace App\Shared\Schema;

use App\Shared\Security\RbacSupport;
use PDO;

final class QuotationSchemaSupport
{
    public static function ensureQuotationsSchema(PDO $pdo): void
    {
        PartySchemaSupport::ensurePartiesSchema($pdo);
        RbacSupport::ensurePermissionsSeed($pdo);

        $pdo->exec("CREATE TABLE IF NOT EXISTS quotations (
            id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
            quotation_no VARCHAR(60) NOT NULL,
            quotation_date DATE NOT NULL,
            valid_until DATE NULL,
            party_id BIGINT UNSIGNED NOT NULL,
            status ENUM('draft','sent','approved','rejected','expired','converted') NOT NULL DEFAULT 'draft',
            subtotal DECIMAL(18,2) NOT NULL DEFAULT 0.00,
            discount_amount DECIMAL(18,2) NOT NULL DEFAULT 0.00,
            tax_amount DECIMAL(18,2) NOT NULL DEFAULT 0.00,
            total_amount DECIMAL(18,2) NOT NULL DEFAULT 0.00,
            notes TEXT NULL,
            payment_terms TEXT NULL,
            delivery_terms TEXT NULL,
            terms_conditions TEXT NULL,
            created_by BIGINT UNSIGNED NULL,
            sent_at DATETIME NULL,
            converted_at DATETIME NULL,
            converted_invoice_id BIGINT UNSIGNED NULL,
            created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            UNIQUE KEY uq_quotation_no (quotation_no),
            KEY idx_quot_party (party_id),
            KEY idx_quot_date (quotation_date)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");

        $pdo->exec("CREATE TABLE IF NOT EXISTS quotation_items (
            id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
            quotation_id BIGINT UNSIGNED NOT NULL,
            item_id BIGINT UNSIGNED NOT NULL,
            unit_id BIGINT UNSIGNED NULL,
            qty DECIMAL(18,3) NOT NULL DEFAULT 1.000,
            unit_price DECIMAL(18,2) NOT NULL DEFAULT 0.00,
            discount_type ENUM('none','percent','amount') NOT NULL DEFAULT 'none',
            discount_value DECIMAL(18,2) NOT NULL DEFAULT 0.00,
            line_total DECIMAL(18,2) NOT NULL DEFAULT 0.00,
            notes VARCHAR(255) NULL,
            created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
            KEY idx_quotation_items_q (quotation_id)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");

        $pdo->exec("CREATE TABLE IF NOT EXISTS quotation_dispatch_logs (
            id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
            quotation_id BIGINT UNSIGNED NOT NULL,
            channel ENUM('email','whatsapp') NOT NULL,
            recipient VARCHAR(190) NOT NULL,
            status VARCHAR(40) NOT NULL DEFAULT 'pending',
            message TEXT NULL,
            sent_by BIGINT UNSIGNED NULL,
            sent_at DATETIME NULL,
            created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
            KEY idx_qdispatch_q (quotation_id)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
    }
}
