CREATE TABLE IF NOT EXISTS `invoice_configs` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(128) NOT NULL,
  `integration_id` INT UNSIGNED DEFAULT NULL,
  `is_delegated` TINYINT(1) NOT NULL DEFAULT 0,
  `is_active` TINYINT(1) NOT NULL DEFAULT 1,
  `number_format` VARCHAR(64) NOT NULL DEFAULT 'FV/%N/%M/%Y',
  `numbering_type` ENUM('monthly','yearly') NOT NULL DEFAULT 'monthly',
  `sale_date_source` ENUM('order_date','payment_date','issue_date') NOT NULL DEFAULT 'issue_date',
  `order_reference` ENUM('none','orderpro','integration') NOT NULL DEFAULT 'none',
  `payment_to_days` TINYINT UNSIGNED NOT NULL DEFAULT 7,
  `default_kind` VARCHAR(32) NOT NULL DEFAULT 'vat',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `invoice_configs_integration_idx` (`integration_id`),
  CONSTRAINT `invoice_configs_integration_fk` FOREIGN KEY (`integration_id`) REFERENCES `integrations` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `invoices` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `order_id` BIGINT UNSIGNED NOT NULL,
  `config_id` INT UNSIGNED NOT NULL,
  `invoice_number` VARCHAR(64) NOT NULL,
  `issue_date` DATETIME NOT NULL,
  `sale_date` DATETIME NOT NULL,
  `payment_due_date` DATETIME DEFAULT NULL,
  `seller_data_json` JSON NOT NULL,
  `buyer_data_json` JSON DEFAULT NULL,
  `items_json` JSON NOT NULL,
  `total_net` DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  `total_gross` DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  `order_reference_value` VARCHAR(128) DEFAULT NULL,
  `external_invoice_id` VARCHAR(128) DEFAULT NULL,
  `external_pdf_url` VARCHAR(500) DEFAULT NULL,
  `kind` VARCHAR(32) NOT NULL DEFAULT 'vat',
  `created_by` INT UNSIGNED DEFAULT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `invoices_number_unique` (`invoice_number`),
  KEY `invoices_order_idx` (`order_id`),
  KEY `invoices_config_date_idx` (`config_id`, `issue_date`),
  KEY `invoices_external_idx` (`external_invoice_id`),
  CONSTRAINT `invoices_order_fk` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `invoices_config_fk` FOREIGN KEY (`config_id`) REFERENCES `invoice_configs` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `invoice_number_counters` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `config_id` INT UNSIGNED NOT NULL,
  `year` SMALLINT UNSIGNED NOT NULL,
  `month` TINYINT UNSIGNED DEFAULT NULL,
  `last_number` INT UNSIGNED NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `invoice_counters_config_period_unique` (`config_id`, `year`, `month`),
  CONSTRAINT `invoice_counters_config_fk` FOREIGN KEY (`config_id`) REFERENCES `invoice_configs` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `fakturownia_integration_settings` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `integration_id` INT UNSIGNED NOT NULL,
  `account_prefix` VARCHAR(64) NOT NULL,
  `api_token_encrypted` TEXT DEFAULT NULL,
  `department_id` VARCHAR(64) DEFAULT NULL,
  `default_kind` VARCHAR(32) NOT NULL DEFAULT 'vat',
  `default_payment_to_days` TINYINT UNSIGNED NOT NULL DEFAULT 7,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `fakturownia_integration_id_unique` (`integration_id`),
  CONSTRAINT `fakturownia_integration_fk` FOREIGN KEY (`integration_id`) REFERENCES `integrations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
