--
-- DeclarationTVA MVP Database Schema
-- Phase 1 - Basic CA-3 Declaration System
-- Simplified for MVP development
--

-- =====================================================
-- 1. CORE MVP TABLES (Simplified)
-- =====================================================

-- Basic configuration table
CREATE TABLE IF NOT EXISTS `llx_declarationtva_config` (
  `rowid` int(11) NOT NULL AUTO_INCREMENT,
  `entity` int(11) NOT NULL DEFAULT 1,
  `config_key` varchar(64) NOT NULL,
  `config_value` text,
  `created_date` datetime DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  UNIQUE KEY `uk_config_entity_key` (`entity`, `config_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- PCG account mappings (multiple accounts per CA-3 line supported)
CREATE TABLE IF NOT EXISTS `llx_declarationtva_account_mappings` (
  `rowid` int(11) NOT NULL AUTO_INCREMENT,
  `entity` int(11) NOT NULL DEFAULT 1,
  `ca3_line` varchar(8) NOT NULL COMMENT 'A1, A2, A3, A4, A5, 08, 09, 9B, 17, 20, 21, 22, 25, 26, 28, 29',
  `account_code` varchar(32) NOT NULL COMMENT 'PCG account code',
  `account_label` varchar(255) DEFAULT NULL,
  `vat_rate` decimal(5,2) DEFAULT NULL,
  `is_active` tinyint(1) DEFAULT 1,
  `created_date` datetime DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  UNIQUE KEY `uk_mapping_entity_line_account` (`entity`, `ca3_line`, `account_code`),
  KEY `idx_ca3_line` (`ca3_line`),
  KEY `idx_account_code` (`account_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Declaration periods (quarterly only for MVP)
CREATE TABLE IF NOT EXISTS `llx_declarationtva_periods` (
  `rowid` int(11) NOT NULL AUTO_INCREMENT,
  `entity` int(11) NOT NULL DEFAULT 1,
  `period_name` varchar(32) NOT NULL COMMENT 'Q1-2024, Q2-2024, etc.',
  `start_date` date NOT NULL,
  `end_date` date NOT NULL,
  `status` varchar(32) DEFAULT 'draft' COMMENT 'draft, validated, submitted',
  `created_date` datetime DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  UNIQUE KEY `uk_period_entity_name` (`entity`, `period_name`),
  KEY `idx_period_dates` (`start_date`, `end_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Main declarations table (simplified)
CREATE TABLE IF NOT EXISTS `llx_declarationtva_declarations` (
  `rowid` int(11) NOT NULL AUTO_INCREMENT,
  `entity` int(11) NOT NULL DEFAULT 1,
  `period_id` int(11) NOT NULL,
  `declaration_number` varchar(32) NOT NULL,
  `status` varchar(32) DEFAULT 'draft' COMMENT 'draft, validated, submitted',
  `total_vat_collected` decimal(15,2) DEFAULT 0.00,
  `total_vat_deductible` decimal(15,2) DEFAULT 0.00,
  `net_vat_due` decimal(15,2) DEFAULT 0.00,
  `vat_credit` decimal(15,2) DEFAULT 0.00,
  `submission_date` datetime DEFAULT NULL,
  `created_date` datetime DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  UNIQUE KEY `uk_declaration_entity_number` (`entity`, `declaration_number`),
  KEY `idx_period_id` (`period_id`),
  CONSTRAINT `fk_declaration_period` FOREIGN KEY (`period_id`) REFERENCES `llx_declarationtva_periods` (`rowid`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- CA-3 form lines (simplified)
CREATE TABLE IF NOT EXISTS `llx_declarationtva_ca3_lines` (
  `rowid` int(11) NOT NULL AUTO_INCREMENT,
  `declaration_id` int(11) NOT NULL,
  `ca3_line` varchar(8) NOT NULL,
  `line_label` varchar(255) DEFAULT NULL,
  `base_amount` decimal(15,2) DEFAULT 0.00,
  `vat_amount` decimal(15,2) DEFAULT 0.00,
  `total_amount` decimal(15,2) DEFAULT 0.00,
  `created_date` datetime DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  UNIQUE KEY `uk_declaration_line` (`declaration_id`, `ca3_line`),
  CONSTRAINT `fk_ca3_declaration` FOREIGN KEY (`declaration_id`) REFERENCES `llx_declarationtva_declarations` (`rowid`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- =====================================================
-- 2. INITIAL DATA FOR MVP
-- =====================================================

-- Insert default configuration
INSERT INTO `llx_declarationtva_config` (`entity`, `config_key`, `config_value`, `created_date`) VALUES
(1, 'module_version', '1.0.0-mvp', NOW()),
(1, 'default_period_type', 'quarterly', NOW()),
(1, 'vat_rates', '20.00,10.00,5.50,2.10,0.00', NOW()),
(1, 'declaration_language', 'fr', NOW());

-- Insert default CA-3 line mappings (simplified)
INSERT INTO `llx_declarationtva_account_mappings` (`entity`, `ca3_line`, `account_code`, `account_label`, `vat_rate`, `created_date`) VALUES
(1, 'A1', '701000', 'Ventes HT 20%', 20.00, NOW()),
(1, 'A2', '701000', 'Opérations spéciales', 20.00, NOW()),
(1, 'B1', '445710', 'TVA collectée 20%', 20.00, NOW()),
(1, 'B2', '445720', 'TVA collectée 10%', 10.00, NOW()),
(1, 'B3', '445730', 'TVA collectée 5.5%', 5.50, NOW()),
(1, 'B4', '445740', 'TVA collectée 2.1%', 2.10, NOW()),
(1, '17', '445200', 'TVA due intra-EU', 20.00, NOW()),
(1, '20', '445620', 'TVA déductible immobilisations', 20.00, NOW()),
(1, '21', '445660', 'TVA déductible autres', 20.00, NOW()),
(1, '22', '445670', 'Crédit TVA', 0.00, NOW()),
(1, '28', '445510', 'TVA nette à payer', 0.00, NOW()),
(1, '29', '445670', 'Crédit TVA à reporter', 0.00, NOW());

-- =====================================================
-- 3. BASIC INDEXES FOR PERFORMANCE
-- =====================================================

CREATE INDEX `idx_declarationtva_periods_dates` ON `llx_declarationtva_periods` (`start_date`, `end_date`);
CREATE INDEX `idx_declarationtva_declarations_period` ON `llx_declarationtva_declarations` (`period_id`);
CREATE INDEX `idx_declarationtva_account_mappings_line` ON `llx_declarationtva_account_mappings` (`ca3_line`);

-- =====================================================
-- END OF MVP SCHEMA
-- =====================================================
