ALTER TABLE `clubs`
  ADD COLUMN IF NOT EXISTS `short_name` varchar(120) NULL,
  ADD COLUMN IF NOT EXISTS `legal_name` varchar(255) NULL,
  ADD COLUMN IF NOT EXISTS `club_number` varchar(64) NULL,
  ADD COLUMN IF NOT EXISTS `email` varchar(255) NULL,
  ADD COLUMN IF NOT EXISTS `phone` varchar(80) NULL,
  ADD COLUMN IF NOT EXISTS `website` varchar(255) NULL,
  ADD COLUMN IF NOT EXISTS `street` varchar(255) NULL,
  ADD COLUMN IF NOT EXISTS `postal_code` varchar(24) NULL,
  ADD COLUMN IF NOT EXISTS `city` varchar(120) NULL,
  ADD COLUMN IF NOT EXISTS `country_code` varchar(2) NOT NULL DEFAULT 'DE',
  ADD COLUMN IF NOT EXISTS `chairperson_name` varchar(255) NULL,
  ADD COLUMN IF NOT EXISTS `treasurer_name` varchar(255) NULL,
  ADD COLUMN IF NOT EXISTS `youth_manager_name` varchar(255) NULL,
  ADD COLUMN IF NOT EXISTS `creditor_identifier` varchar(64) NULL,
  ADD COLUMN IF NOT EXISTS `billing_email` varchar(255) NULL,
  ADD COLUMN IF NOT EXISTS `iban` varchar(34) NULL,
  ADD COLUMN IF NOT EXISTS `bic` varchar(11) NULL,
  ADD COLUMN IF NOT EXISTS `fee_rules` json NULL,
  ADD COLUMN IF NOT EXISTS `outgoing_invoice_prefix` varchar(24) NOT NULL DEFAULT 'RE',
  ADD COLUMN IF NOT EXISTS `outgoing_invoice_next_number` int NOT NULL DEFAULT 1,
  ADD COLUMN IF NOT EXISTS `incoming_invoice_prefix` varchar(24) NOT NULL DEFAULT 'EI',
  ADD COLUMN IF NOT EXISTS `incoming_invoice_next_number` int NOT NULL DEFAULT 1,
  ADD COLUMN IF NOT EXISTS `is_archived` tinyint(1) NOT NULL DEFAULT 0,
  ADD COLUMN IF NOT EXISTS `archived_at` datetime NULL;

ALTER TABLE `member`
  ADD COLUMN IF NOT EXISTS `member_number` varchar(64) NULL,
  ADD COLUMN IF NOT EXISTS `membership_status` varchar(32) NOT NULL DEFAULT 'active',
  ADD COLUMN IF NOT EXISTS `membership_type` varchar(32) NOT NULL DEFAULT 'regular',
  ADD COLUMN IF NOT EXISTS `joined_on` date NULL,
  ADD COLUMN IF NOT EXISTS `left_on` date NULL,
  ADD COLUMN IF NOT EXISTS `contribution_group_code` varchar(64) NULL,
  ADD COLUMN IF NOT EXISTS `needs_sepa_mandate` tinyint(1) NOT NULL DEFAULT 0,
  ADD COLUMN IF NOT EXISTS `sepa_mandate_reference` varchar(80) NULL,
  ADD COLUMN IF NOT EXISTS `is_archived` tinyint(1) NOT NULL DEFAULT 0,
  ADD COLUMN IF NOT EXISTS `archived_at` datetime NULL,
  ADD COLUMN IF NOT EXISTS `archived_reason` text NULL;

CREATE TABLE IF NOT EXISTS `club_requests` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `request_type` varchar(32) NOT NULL,
  `status` varchar(32) NOT NULL DEFAULT 'open',
  `workflow_stage` varchar(64) NULL,
  `priority` varchar(16) NOT NULL DEFAULT 'normal',
  `subject` varchar(255) NULL,
  `first_name` varchar(120) NULL,
  `last_name` varchar(120) NULL,
  `email` varchar(255) NULL,
  `phone` varchar(80) NULL,
  `message` text NULL,
  `source_system` varchar(64) NULL,
  `received_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `assigned_user_id` bigint NULL,
  `assigned_member_id` bigint NULL,
  `converted_member_id` bigint NULL,
  `closed_at` datetime 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_club_requests_club_status` (`club_id`, `status`, `request_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_distribution_groups` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` text NULL,
  `group_type` varchar(32) NOT NULL DEFAULT 'custom',
  `is_system_group` tinyint(1) 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_club_distribution_groups_club` (`club_id`, `name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_distribution_group_members` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `group_id` bigint NOT NULL,
  `member_id` bigint NOT 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_club_distribution_group_member` (`group_id`, `member_id`),
  KEY `idx_club_distribution_group_members_member` (`member_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_communication_threads` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `thread_type` varchar(32) NOT NULL DEFAULT 'direct',
  `subject` varchar(255) NOT NULL,
  `status` varchar(32) NOT NULL DEFAULT 'draft',
  `created_by_user_id` bigint NULL,
  `distribution_group_id` bigint NULL,
  `recipient_member_id` bigint NULL,
  `scheduled_at` datetime NULL,
  `sent_at` datetime 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_club_communication_threads_club` (`club_id`, `status`, `thread_type`),
  KEY `idx_club_communication_threads_group` (`distribution_group_id`),
  KEY `idx_club_communication_threads_member` (`recipient_member_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_communication_messages` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `thread_id` bigint NOT NULL,
  `club_id` bigint NOT NULL,
  `message_type` varchar(32) NOT NULL DEFAULT 'message',
  `direction` varchar(32) NOT NULL DEFAULT 'outbound',
  `body` text NOT NULL,
  `created_by_user_id` bigint 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_club_communication_messages_thread` (`thread_id`, `created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_communication_recipients` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `thread_id` bigint NOT NULL,
  `club_id` bigint NOT NULL,
  `member_id` bigint NULL,
  `recipient_name` varchar(255) NOT NULL,
  `email_snapshot` varchar(255) NULL,
  `delivery_status` varchar(32) NOT NULL DEFAULT 'pending',
  `delivered_at` datetime NULL,
  `last_attempt_at` datetime NULL,
  `attempt_count` int NOT NULL DEFAULT 0,
  `retryable` tinyint(1) NOT NULL DEFAULT 0,
  `error_code` varchar(64) NULL,
  `transport_message_id` 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_club_communication_recipients_thread` (`thread_id`, `delivery_status`),
  KEY `idx_club_communication_recipients_member` (`member_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_communication_delivery_logs` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `thread_id` bigint NOT NULL,
  `recipient_id` bigint NULL,
  `created_by_user_id` bigint NULL,
  `status` varchar(32) NOT NULL DEFAULT 'failed',
  `attempt_no` int NOT NULL DEFAULT 1,
  `retryable` tinyint(1) NOT NULL DEFAULT 0,
  `error_code` varchar(64) NULL,
  `error_message` text NULL,
  `transport_message_id` varchar(255) NULL,
  `transport_response` 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_club_communication_delivery_logs_thread` (`thread_id`, `created_at`),
  KEY `idx_club_communication_delivery_logs_recipient` (`recipient_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `club_distribution_groups`
  ADD COLUMN IF NOT EXISTS `filter_definition` json NULL;

ALTER TABLE `club_communication_threads`
  ADD COLUMN IF NOT EXISTS `recipient_filters` json NULL;

CREATE TABLE IF NOT EXISTS `club_communication_templates` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `name` varchar(160) NOT NULL,
  `category` varchar(64) NOT NULL DEFAULT 'general',
  `subject_template` varchar(255) NULL,
  `body_template` text NULL,
  `variables_hint` 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_club_communication_templates_club` (`club_id`, `category`, `name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_account_transactions` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `account_id` bigint NOT NULL,
  `invoice_id` bigint NULL,
  `created_by_user_id` bigint NULL,
  `direction` varchar(16) NOT NULL DEFAULT 'credit',
  `booking_type` varchar(32) NOT NULL DEFAULT 'manual',
  `status` varchar(32) NOT NULL DEFAULT 'booked',
  `booking_date` date NOT NULL,
  `value_date` date NULL,
  `amount_cents` bigint NOT NULL DEFAULT 0,
  `currency_code` varchar(3) NOT NULL DEFAULT 'EUR',
  `reference` varchar(255) NULL,
  `notes` 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_club_account_transactions_club_date` (`club_id`, `booking_date`, `status`),
  KEY `idx_club_account_transactions_account_date` (`account_id`, `booking_date`),
  KEY `idx_club_account_transactions_invoice` (`invoice_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_request_notes` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_request_id` bigint NOT NULL,
  `note_type` varchar(32) NOT NULL DEFAULT 'internal',
  `body` text NOT NULL,
  `created_by_user_id` bigint NULL,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_club_request_notes_request` (`club_request_id`, `created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_tasks` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `title` varchar(255) NOT NULL,
  `task_type` varchar(64) NULL,
  `description` text NULL,
  `status` varchar(32) NOT NULL DEFAULT 'open',
  `priority` varchar(16) NOT NULL DEFAULT 'normal',
  `due_at` datetime NULL,
  `remind_at` datetime NULL,
  `created_by_user_id` bigint NULL,
  `assigned_user_id` bigint NULL,
  `automation_source` varchar(64) NULL,
  `automation_key` varchar(255) NULL,
  `related_entity_type` varchar(32) NULL,
  `related_entity_id` bigint NULL,
  `completed_at` datetime NULL,
  `archived_at` datetime NULL,
  `source_snapshot` json 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_club_tasks_club_status_due` (`club_id`, `status`, `due_at`),
  UNIQUE KEY `uq_club_tasks_automation_key` (`club_id`, `automation_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_task_suppressions` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `automation_key` varchar(255) NOT NULL,
  `suppression_token` varchar(255) NOT NULL,
  `dismissed_by_user_id` bigint 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 `uq_club_task_suppressions_key` (`club_id`, `automation_key`),
  KEY `idx_club_task_suppressions_lookup` (`club_id`, `automation_key`, `suppression_token`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_roles` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `role_key` varchar(64) NOT NULL,
  `name` varchar(120) NOT NULL,
  `description` text NULL,
  `permissions` json NOT NULL,
  `is_system_role` tinyint(1) NOT NULL DEFAULT 0,
  `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`),
  UNIQUE KEY `uq_club_roles_key` (`club_id`, `role_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_user_roles` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `user_id` bigint NOT NULL,
  `club_role_id` bigint NOT NULL,
  `is_primary` tinyint(1) 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`),
  UNIQUE KEY `uq_club_user_roles_assignment` (`club_id`, `user_id`, `club_role_id`),
  KEY `idx_club_user_roles_user` (`club_id`, `user_id`),
  KEY `idx_club_user_roles_role` (`club_role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_sepa_mandates` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `member_id` bigint NULL,
  `debtor_name` varchar(255) NOT NULL,
  `iban` varchar(34) NOT NULL,
  `bic` varchar(11) NULL,
  `mandate_reference` varchar(80) NOT NULL,
  `signed_on` date NULL,
  `valid_from` date NULL,
  `revoked_at` datetime NULL,
  `status` varchar(32) NOT NULL DEFAULT 'active',
  `history_note` 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_club_sepa_mandates_club_member` (`club_id`, `member_id`, `status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_payment_claims` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `member_id` bigint NULL,
  `fee_rule_id` bigint NULL,
  `claim_type` varchar(32) NOT NULL DEFAULT 'membership_fee',
  `status` varchar(32) NOT NULL DEFAULT 'open',
  `due_on` date NOT NULL,
  `amount_cents` bigint NOT NULL,
  `currency_code` varchar(3) NOT NULL DEFAULT 'EUR',
  `reminder_level` int NOT NULL DEFAULT 0,
  `last_reminder_at` datetime NULL,
  `notes` text NULL,
  `settled_at` datetime NULL,
  `archived_at` datetime 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_club_payment_claims_club_status_due` (`club_id`, `status`, `due_on`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_accounts` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `name` varchar(160) NOT NULL,
  `account_holder` varchar(255) NULL,
  `bank_name` varchar(160) NULL,
  `iban` varchar(34) NULL,
  `bic` varchar(11) NULL,
  `account_type` varchar(16) NOT NULL DEFAULT 'bank',
  `usage_type` varchar(32) NOT NULL DEFAULT 'general',
  `currency_code` varchar(3) NOT NULL DEFAULT 'EUR',
  `allow_sepa_collections` tinyint(1) NOT NULL DEFAULT 0,
  `allow_outgoing_payments` tinyint(1) NOT NULL DEFAULT 1,
  `is_default` tinyint(1) NOT NULL DEFAULT 0,
  `status` varchar(16) NOT NULL DEFAULT 'active',
  `notes` text NULL,
  `sort_order` int NOT NULL DEFAULT 0,
  `archived_at` datetime 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_club_accounts_club_status` (`club_id`, `status`, `account_type`),
  KEY `idx_club_accounts_default` (`club_id`, `is_default`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_invoice_parties` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `party_type` varchar(32) NOT NULL DEFAULT 'customer',
  `status` varchar(32) NOT NULL DEFAULT 'active',
  `name` varchar(255) NOT NULL,
  `contract_reference` varchar(120) NULL,
  `valid_from` date NULL,
  `valid_to` date NULL,
  `contact_name` varchar(255) NULL,
  `email` varchar(255) NULL,
  `phone` varchar(80) NULL,
  `street` varchar(255) NULL,
  `postal_code` varchar(24) NULL,
  `city` varchar(120) NULL,
  `country_code` varchar(2) NOT NULL DEFAULT 'DE',
  `iban` varchar(34) NULL,
  `bic` varchar(11) NULL,
  `tax_identifier` varchar(64) NULL,
  `notes` 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_club_invoice_parties_club_type` (`club_id`, `party_type`),
  KEY `idx_club_invoice_parties_club_status` (`club_id`, `status`),
  KEY `idx_club_invoice_parties_club_valid_from` (`club_id`, `valid_from`),
  KEY `idx_club_invoice_parties_club_valid_to` (`club_id`, `valid_to`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `club_invoices` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `invoice_direction` varchar(16) NOT NULL,
  `invoice_type` varchar(32) NOT NULL,
  `status` varchar(32) NOT NULL DEFAULT 'draft',
  `invoice_number` varchar(64) NULL,
  `external_reference` varchar(255) NULL,
  `party_id` bigint NULL,
  `account_id` bigint NULL,
  `issued_on` date NULL,
  `due_on` date NULL,
  `paid_on` date NULL,
  `net_amount_cents` bigint NOT NULL DEFAULT 0,
  `tax_amount_cents` bigint NOT NULL DEFAULT 0,
  `gross_amount_cents` bigint NOT NULL DEFAULT 0,
  `currency_code` varchar(3) NOT NULL DEFAULT 'EUR',
  `description` text NULL,
  `document_id` bigint NULL,
  `created_by_user_id` bigint NULL,
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `archived_at` datetime NULL,
  PRIMARY KEY (`id`),
  KEY `idx_club_invoices_club_direction_status` (`club_id`, `invoice_direction`, `status`, `due_on`),
  UNIQUE KEY `uq_club_invoices_number` (`club_id`, `invoice_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

SET @club_invoices_number_index_exists := (
  SELECT COUNT(*)
  FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'club_invoices'
    AND index_name = 'uq_club_invoices_number'
);
SET @club_invoices_number_index_sql := IF(
  @club_invoices_number_index_exists = 0,
  'ALTER TABLE `club_invoices` ADD UNIQUE KEY `uq_club_invoices_number` (`club_id`, `invoice_number`)',
  'SELECT 1'
);
PREPARE club_invoices_number_index_stmt FROM @club_invoices_number_index_sql;
EXECUTE club_invoices_number_index_stmt;
DEALLOCATE PREPARE club_invoices_number_index_stmt;

CREATE TABLE IF NOT EXISTS `club_invoice_items` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `invoice_id` bigint NOT NULL,
  `line_no` int NOT NULL DEFAULT 1,
  `description` text NOT NULL,
  `quantity` decimal(12,2) NOT NULL DEFAULT 1.00,
  `unit_price_cents` bigint NOT NULL DEFAULT 0,
  `tax_rate` decimal(5,2) NOT NULL DEFAULT 0.00,
  `total_cents` bigint 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`),
  UNIQUE KEY `uq_club_invoice_items_line` (`invoice_id`, `line_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `club_communication_recipients`
  ADD COLUMN IF NOT EXISTS `last_attempt_at` datetime NULL,
  ADD COLUMN IF NOT EXISTS `attempt_count` int NOT NULL DEFAULT 0,
  ADD COLUMN IF NOT EXISTS `retryable` tinyint(1) NOT NULL DEFAULT 0,
  ADD COLUMN IF NOT EXISTS `error_code` varchar(64) NULL,
  ADD COLUMN IF NOT EXISTS `transport_message_id` varchar(255) NULL;

CREATE TABLE IF NOT EXISTS `club_communication_delivery_logs` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `club_id` bigint NOT NULL,
  `thread_id` bigint NOT NULL,
  `recipient_id` bigint NULL,
  `created_by_user_id` bigint NULL,
  `status` varchar(32) NOT NULL DEFAULT 'failed',
  `attempt_no` int NOT NULL DEFAULT 1,
  `retryable` tinyint(1) NOT NULL DEFAULT 0,
  `error_code` varchar(64) NULL,
  `error_message` text NULL,
  `transport_message_id` varchar(255) NULL,
  `transport_response` 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_club_communication_delivery_logs_thread` (`thread_id`, `created_at`),
  KEY `idx_club_communication_delivery_logs_recipient` (`recipient_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
