CREATE TABLE IF NOT EXISTS shipment_packages (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    order_id BIGINT UNSIGNED NOT NULL,
    provider VARCHAR(32) NOT NULL DEFAULT 'allegro_wza',
    delivery_method_id VARCHAR(128) NULL,
    credentials_id VARCHAR(128) NULL,
    command_id VARCHAR(64) NULL,
    shipment_id VARCHAR(64) NULL,
    tracking_number VARCHAR(128) NULL,
    status VARCHAR(32) NOT NULL DEFAULT 'draft',
    carrier_id VARCHAR(64) NULL,
    package_type VARCHAR(16) NOT NULL DEFAULT 'PACKAGE',
    weight_kg DECIMAL(8,3) NULL,
    length_cm DECIMAL(8,1) NULL,
    width_cm DECIMAL(8,1) NULL,
    height_cm DECIMAL(8,1) NULL,
    insurance_amount DECIMAL(12,2) NULL,
    insurance_currency CHAR(3) NULL,
    cod_amount DECIMAL(12,2) NULL,
    cod_currency CHAR(3) NULL,
    label_format VARCHAR(8) NOT NULL DEFAULT 'PDF',
    label_path VARCHAR(512) NULL,
    receiver_point_id VARCHAR(64) NULL,
    sender_point_id VARCHAR(64) NULL,
    reference_number VARCHAR(128) NULL,
    error_message TEXT 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 shipment_packages_order_idx (order_id),
    KEY shipment_packages_status_idx (status),
    KEY shipment_packages_tracking_idx (tracking_number),
    KEY shipment_packages_command_idx (command_id),
    CONSTRAINT shipment_packages_order_fk
        FOREIGN KEY (order_id) REFERENCES orders(id)
        ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
