-- Migration: Add documents table for linking validated PDFs to declarations
-- Version: 2.1.0
-- Date: 2025-01-06

-- Create table for linking documents to declarations
CREATE TABLE IF NOT EXISTS `llx_declarationtva_documents` (
  `rowid` int(11) NOT NULL AUTO_INCREMENT,
  `declaration_id` int(11) NOT NULL,
  `document_id` int(11) NOT NULL,
  `created_date` datetime NOT NULL,
  `entity` int(11) NOT NULL DEFAULT 1,
  PRIMARY KEY (`rowid`),
  KEY `idx_declaration_id` (`declaration_id`),
  KEY `idx_document_id` (`document_id`),
  KEY `idx_entity` (`entity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Add validated_date and validated_by columns to declarations table if they don't exist
ALTER TABLE `llx_declarationtva_declarations` 
ADD COLUMN IF NOT EXISTS `validated_date` datetime DEFAULT NULL,
ADD COLUMN IF NOT EXISTS `validated_by` int(11) DEFAULT NULL;

-- Add indexes for performance
ALTER TABLE `llx_declarationtva_declarations` 
ADD INDEX IF NOT EXISTS `idx_validated_date` (`validated_date`),
ADD INDEX IF NOT EXISTS `idx_validated_by` (`validated_by`);
