CREATE TABLE IF NOT EXISTS orders (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    integration_id INT UNSIGNED NOT NULL,
    external_order_id VARCHAR(64) NOT NULL,
    external_order_number VARCHAR(128) NULL,
    status VARCHAR(64) NULL,
    currency CHAR(3) NULL,
    total_gross DECIMAL(12,2) NULL,
    total_net DECIMAL(12,2) NULL,
    buyer_email VARCHAR(190) NULL,
    buyer_name VARCHAR(190) NULL,
    buyer_phone VARCHAR(64) NULL,
    payment_method VARCHAR(128) NULL,
    payment_status VARCHAR(64) NULL,
    delivery_method VARCHAR(128) NULL,
    delivery_price DECIMAL(12,2) NULL,
    delivery_tracking_number VARCHAR(128) NULL,
    notes TEXT NULL,
    external_created_at DATETIME NULL,
    external_updated_at DATETIME NULL,
    payload_json JSON NULL,
    fetched_at DATETIME NOT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY orders_integration_external_unique (integration_id, external_order_id),
    KEY orders_integration_updated_idx (integration_id, external_updated_at),
    KEY orders_status_idx (status),
    KEY orders_fetched_at_idx (fetched_at),
    CONSTRAINT orders_integration_fk
        FOREIGN KEY (integration_id) REFERENCES integrations(id)
        ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS order_items (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    order_id INT UNSIGNED NOT NULL,
    external_item_id VARCHAR(64) NULL,
    name VARCHAR(255) NOT NULL,
    sku VARCHAR(128) NULL,
    ean VARCHAR(64) NULL,
    quantity DECIMAL(12,3) NOT NULL DEFAULT 0,
    price_gross DECIMAL(12,2) NULL,
    price_net DECIMAL(12,2) NULL,
    vat DECIMAL(6,2) NULL,
    payload_json JSON NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    KEY order_items_order_idx (order_id),
    KEY order_items_external_item_idx (external_item_id),
    CONSTRAINT order_items_order_fk
        FOREIGN KEY (order_id) REFERENCES orders(id)
        ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS integration_order_sync_state (
    integration_id INT UNSIGNED PRIMARY KEY,
    last_synced_external_updated_at DATETIME NULL,
    last_synced_external_order_id VARCHAR(64) NULL,
    last_run_at DATETIME NULL,
    last_error VARCHAR(500) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT integration_order_sync_state_integration_fk
        FOREIGN KEY (integration_id) REFERENCES integrations(id)
        ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO cron_schedules (
    job_type, interval_seconds, priority, max_attempts, payload, enabled, last_run_at, next_run_at, created_at, updated_at
) VALUES (
    'shoppro_orders_import',
    60,
    90,
    3,
    NULL,
    1,
    NULL,
    NOW(),
    NOW(),
    NOW()
)
ON DUPLICATE KEY UPDATE
    interval_seconds = VALUES(interval_seconds),
    priority = VALUES(priority),
    max_attempts = VALUES(max_attempts),
    payload = VALUES(payload),
    enabled = VALUES(enabled),
    updated_at = VALUES(updated_at);
