"""Peewee migrations -- 194_MAIN.py.

Split ``TaxDocument.date`` (was "issue + VAT date") into two explicit dates:
rename it to ``t_issue`` ("datum vystavení") and add ``t_tax`` (DUZP),
backfilled from the old ``date`` value.
"""

import peewee as pw
from peewee_migrate import Migrator


def migrate(migrator: Migrator, database: pw.Database, *, fake: bool = False) -> None:
    migrator.rename_field("taxdocument", "date", "t_issue")
    migrator.add_fields(
        "taxdocument",
        t_tax=pw.IntegerField(constraints=[pw.SQL("DEFAULT 0")], default=0),
    )
    migrator.sql("UPDATE taxdocument SET t_tax = t_issue")


def rollback(migrator: Migrator, database: pw.Database, *, fake: bool = False) -> None:
    migrator.remove_fields("taxdocument", "t_tax")
    migrator.rename_field("taxdocument", "t_issue", "date")
