-- Abrechnungsmodul: Vorlagen, Feld-Mapping, Abrechnungslauf und erzeugte Dokumente

CREATE TABLE IF NOT EXISTS `billing_template` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `club_id` INT NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `description` TEXT NULL,
  `pdf_storage_path` VARCHAR(1000) NOT NULL,
  `pdf_filename` VARCHAR(255) NOT NULL,
  `pdf_mime_type` VARCHAR(100) NOT NULL DEFAULT 'application/pdf',
  `is_active` TINYINT(1) NOT NULL DEFAULT 1,
  `version` INT NOT NULL DEFAULT 1,
  `created_by_user_id` INT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_billing_template_club` (`club_id`),
  UNIQUE KEY `uniq_billing_template_club_name_version` (`club_id`, `name`, `version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `billing_template_field` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `template_id` INT NOT NULL,
  `field_key` VARCHAR(120) NOT NULL,
  `label` VARCHAR(255) NOT NULL,
  `field_type` ENUM('text', 'number', 'currency', 'date', 'checkbox', 'formula', 'table_row') NOT NULL,
  `source_type` ENUM('manual', 'member', 'trainer', 'club', 'system', 'constant', 'formula') NOT NULL DEFAULT 'manual',
  `source_path` VARCHAR(255) NULL,
  `constant_value` VARCHAR(500) NULL,
  `formatter` ENUM('none', 'iban_no_country', 'date_dd_mm_yyyy', 'currency_eur_2') NOT NULL DEFAULT 'none',
  `is_required` TINYINT(1) NOT NULL DEFAULT 0,
  `mapping_mode` ENUM('acroform', 'overlay') NOT NULL DEFAULT 'overlay',
  `acroform_field_name` VARCHAR(255) NULL,
  `page_number` INT NULL,
  `x` DECIMAL(10, 2) NULL,
  `y` DECIMAL(10, 2) NULL,
  `width` DECIMAL(10, 2) NULL,
  `height` DECIMAL(10, 2) NULL,
  `font_size` DECIMAL(5, 2) NULL,
  `align` ENUM('left', 'center', 'right') NULL,
  `formula_expression` VARCHAR(1000) NULL,
  `table_group` VARCHAR(100) NULL,
  `row_index` INT NULL,
  `sort_order` INT NOT NULL DEFAULT 0,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_billing_template_field_template` (`template_id`),
  UNIQUE KEY `uniq_billing_template_field_key` (`template_id`, `field_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `billing_run` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `club_id` INT NOT NULL,
  `template_id` INT NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `period_start` DATE NOT NULL,
  `period_end` DATE NOT NULL,
  `self_recipient_user_id` INT NOT NULL,
  `self_recipient_name` VARCHAR(255) NOT NULL,
  `hourly_rate` DECIMAL(10, 2) NOT NULL,
  `computed_hours_total` DECIMAL(10, 2) NOT NULL DEFAULT 0,
  `iban` VARCHAR(64) NULL,
  `iban_without_country` TINYINT(1) NOT NULL DEFAULT 0,
  `session_label` VARCHAR(255) NULL,
  `same_account_checkbox` TINYINT(1) NOT NULL DEFAULT 0,
  `omit_self_recipient_name` TINYINT(1) NOT NULL DEFAULT 0,
  `omit_iban` TINYINT(1) NOT NULL DEFAULT 0,
  `omit_location_text` TINYINT(1) NOT NULL DEFAULT 0,
  `omit_document_date` TINYINT(1) NOT NULL DEFAULT 0,
  `omit_session_label` TINYINT(1) NOT NULL DEFAULT 0,
  `location_text` VARCHAR(255) NULL,
  `document_date` DATE NULL,
  `status` ENUM('draft', 'generated', 'finalized', 'cancelled') NOT NULL DEFAULT 'draft',
  `created_by_user_id` INT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_billing_run_club` (`club_id`),
  KEY `idx_billing_run_template` (`template_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `billing_user_setting` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `club_id` INT NOT NULL,
  `user_id` INT NOT NULL,
  `last_hourly_rate` DECIMAL(10, 2) NOT NULL DEFAULT 0,
  `last_self_recipient_name` VARCHAR(255) NULL,
  `last_location_text` VARCHAR(255) NULL,
  `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 `uniq_billing_user_setting_club_user` (`club_id`, `user_id`),
  KEY `idx_billing_user_setting_club` (`club_id`),
  KEY `idx_billing_user_setting_user` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `billing_document` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `run_id` INT NOT NULL,
  `display_name` VARCHAR(255) NOT NULL,
  `status` ENUM('draft', 'generated', 'error') NOT NULL DEFAULT 'draft',
  `pdf_storage_path` VARCHAR(1000) NULL,
  `pdf_filename` VARCHAR(255) NULL,
  `error_message` TEXT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_billing_document_run` (`run_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `billing_document_value` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `billing_document_id` INT NOT NULL,
  `field_key` VARCHAR(120) NOT NULL,
  `resolved_value` TEXT NULL,
  `resolved_source` VARCHAR(255) NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_billing_doc_value_doc` (`billing_document_id`),
  UNIQUE KEY `uniq_billing_doc_value_field` (`billing_document_id`, `field_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
