<?php
require 'includes/db.php';

try {
    echo "Dropping production_invoices table...\n";
    $pdo->exec("DROP TABLE IF EXISTS production_invoices");

    echo "Creating production_invoices table...\n";
    $pdo->exec("
        CREATE TABLE production_invoices (
            id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            company_id BIGINT UNSIGNED NOT NULL,
            production_line_id BIGINT UNSIGNED NOT NULL,
            product_id BIGINT UNSIGNED NOT NULL,
            buyer_id BIGINT UNSIGNED NULL,
            units_completed INT NOT NULL,
            unit_price DECIMAL(15,2) NOT NULL,
            total_amount DECIMAL(15,2) NOT NULL,
            invoice_number VARCHAR(50) NOT NULL,
            buyer_details TEXT DEFAULT NULL,
            po_reference VARCHAR(100) DEFAULT NULL,
            product_details TEXT DEFAULT NULL,
            taxes_vat DECIMAL(15,2) DEFAULT NULL,
            discounts DECIMAL(15,2) DEFAULT NULL,
            shipping_charges DECIMAL(15,2) DEFAULT NULL,
            payment_terms VARCHAR(100) DEFAULT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
            UNIQUE KEY uq_invoice_num (invoice_number)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    ");
    echo "Table production_invoices created successfully!\n";
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
