-- Invoices: rich-text "Obsah" sections (mirrors issued_order_sections) replace
-- the printed notes block; notes become internal-only. Existing printed notes
-- are preserved by merging them into internal_notes before the column drop
-- (both are Quill HTML — concatenation is valid markup).

-- Preserve data: move printed notes into internal_notes
UPDATE `invoices`
SET `internal_notes` = CASE
    WHEN `internal_notes` IS NULL OR `internal_notes` = '' THEN `notes`
    ELSE CONCAT(`internal_notes`, `notes`)
  END
WHERE `notes` IS NOT NULL AND `notes` <> '';

-- AlterTable
ALTER TABLE `invoices` DROP COLUMN `notes`;

-- CreateTable
CREATE TABLE `invoice_sections` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `invoice_id` INTEGER NOT NULL,
    `position` INTEGER NULL DEFAULT 0,
    `title` VARCHAR(500) NULL,
    `title_cz` VARCHAR(500) NULL,
    `content` TEXT NULL,
    `modified_at` DATETIME(0) NULL,

    INDEX `invoice_sections_invoice_id`(`invoice_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `invoice_sections` ADD CONSTRAINT `invoice_sections_fk` FOREIGN KEY (`invoice_id`) REFERENCES `invoices`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
