from sqlalchemy import Column, MetaData, Table
from sqlalchemy.dialects import postgresql
from sqlalchemy.sql.schema import UniqueConstraint
from sqlalchemy.sql.sqltypes import (
    Boolean,
    Date,
    DateTime,
    Float,
    Integer,
    Numeric,
    SmallInteger,
    String,
    Text,
)

array_ish = Text()
array_ish = array_ish.with_variant(postgresql.ARRAY(Integer), "postgresql")


def bitmap_ish(n):
    tp = Text()
    return tp.with_variant(postgresql.BIT(n, varying=True), "postgresql")


meta = MetaData()

schema = [
    Table(
        "prezident_davky",
        meta,
        Column("datum", Date, nullable=False),
        Column("kolo", SmallInteger, nullable=False),
        Column("cis_obec", Integer, nullable=False),
        Column("cis_okrsek", Integer, nullable=False),
        Column("poradi_zprac", SmallInteger, nullable=False),
        Column("datum_cas_zprac", DateTime, nullable=False),
        Column("opakovane", Boolean, nullable=False),
    ),
    Table(
        "prezident_kandidati",
        meta,
        Column("datum", Date, nullable=False),
        Column("ckand", SmallInteger, nullable=False),
        Column("jmeno", Text, nullable=False),
        Column("prijmeni", Text, nullable=False),
        Column("titulpred", Text, nullable=True),
        Column("titulza", Text, nullable=True),
        Column("vek", SmallInteger, nullable=False),
        Column("povolani", Text, nullable=False),
        Column("bydlisten", Text, nullable=False),
        Column("bydlistek", Integer, nullable=False),
        Column("pstrana", SmallInteger, nullable=False),
        Column("nstrana", SmallInteger, nullable=False),
        Column("pohlavi", SmallInteger, nullable=False),
        Column("platnost", Text, nullable=False),
        Column("hlasy_k1", Integer, nullable=False),
        Column("zvolen_k1", SmallInteger, nullable=False),
        Column("hlasy_k2", Integer, nullable=False),
        Column("zvolen_k2", SmallInteger, nullable=False),
        UniqueConstraint("datum", "ckand"),
    ),
    Table(
        "prezident_strany",
        meta,
        Column("datum", Date, nullable=False),
        Column("nstrana", SmallInteger, nullable=False),
        Column("nazev_strn", Text, nullable=False),
        Column("zkratkan30", Text, nullable=False),
        Column("zkratkan8", Text, nullable=False),
        UniqueConstraint("datum", "nstrana"),
    ),
    Table(
        "prezident_okrsky",
        meta,
        Column("datum", Date, nullable=False),
        Column("typ_form", Integer, nullable=False),
        Column("oprava", Integer, nullable=False),
        Column("chyba", Integer, nullable=False),
        Column("okres", Integer, nullable=False),
        Column("obec", Integer, nullable=False),
        Column("okrsek", Integer, nullable=False),
        Column("kc_1", Integer, nullable=False),
        Column("kolo", SmallInteger, nullable=False),
        Column("vol_seznam", Integer, nullable=False),
        Column("vyd_obalky", Integer, nullable=False),
        Column("odevz_obal", Integer, nullable=False),
        Column("pl_hl_celk", Integer, nullable=False),
        Column("kc_2", Integer, nullable=False),
        Column("kc_3", Integer, nullable=False),
        Column("kc_4", Integer, nullable=False),
        Column("posl_kand", SmallInteger, nullable=False),
        Column("kc_sum", Integer, nullable=False),
        Column("hlasy", array_ish, nullable=False),
        UniqueConstraint("datum", "okres", "obec", "okrsek", "kolo"),
    ),
    Table(
        "prezident_nuts",
        meta,
        Column("datum", Date, nullable=False),
        Column("num_nuts", Integer, nullable=False),
        Column("nuts", Text, nullable=True),  # kvuli zahranicnimu "okresu"
        Column("nazev", Text, nullable=False),
        Column("ciselnik_kod", Integer, nullable=True),
        Column("ciselnik_hodnota", Integer, nullable=True),
        UniqueConstraint("datum", "num_nuts"),
    ),
    Table(
        "prezident_obce",
        meta,
        Column("datum", Date, nullable=False),
        Column("kraj", Integer, nullable=False),
        Column("okres", Integer, nullable=False),
        Column("cpou", Integer, nullable=False),
        Column("orp", Integer, nullable=True),
        Column("obec", Integer, nullable=False),
        Column("nazevobce", Text, nullable=False),
        Column("obec_prez", Integer, nullable=False),
        UniqueConstraint("datum", "kraj", "okres", "obec"),
    ),
    Table(
        "psp_davky",
        meta,
        Column("datum", Date, nullable=False),
        Column("cis_obec", Integer, nullable=False),
        Column("cis_okrsek", Integer, nullable=False),
        Column("poradi_zprac", SmallInteger, nullable=False),
        Column("datum_cas_zprac", DateTime, nullable=False),
        Column("opakovane", Boolean, nullable=False),
    ),
    Table(
        "psp_kandidati",
        meta,
        Column("datum", Date, nullable=False),
        Column("volkraj", SmallInteger, nullable=False),
        Column("kstrana", SmallInteger, nullable=False),
        Column("porcislo", SmallInteger, nullable=False),
        Column("jmeno", Text, nullable=False),
        Column("prijmeni", Text, nullable=False),
        Column("titulpred", Text, nullable=True),
        Column("titulza", Text, nullable=True),
        Column("vek", SmallInteger, nullable=True),
        Column("povolani", Text, nullable=True),
        Column("bydlisten", Text, nullable=True),
        Column("bydlistek", Integer, nullable=True),
        Column("pstrana", SmallInteger, nullable=True),
        Column("nstrana", SmallInteger, nullable=True),
        Column("platnost", Text, nullable=False),
        Column("pochlasu", Integer, nullable=False),
        Column("pocproc", Numeric(5, 2), nullable=False),
        Column("pocprocvse", Numeric(5, 2), nullable=True),
        Column("mandat", Boolean, nullable=False),
        Column("skrutinium", SmallInteger, nullable=True),
        Column("poradimand", SmallInteger, nullable=True),
        Column("poradinahr", SmallInteger, nullable=True),
        UniqueConstraint("datum", "volkraj", "kstrana", "porcislo"),
    ),
    Table(
        "psp_strany",
        meta,
        Column("datum", Date, nullable=False),
        Column("kstrana", SmallInteger, nullable=False),
        Column("vstrana", SmallInteger, nullable=False),
        Column("nazevcelk", Text, nullable=False),
        Column("nazev_strk", Text, nullable=False),
        Column("zkratkak30", Text, nullable=False),
        Column("zkratkak8", Text, nullable=False),
        Column("pocstrvko", SmallInteger, nullable=False),
        Column("slozeni", Text, nullable=False),
        Column("stavreg", Text, nullable=False),
        Column("platnost", Text, nullable=True),
        Column("plat_str", Text, nullable=False),
        Column("pocmandat", SmallInteger, nullable=True),
        Column("pocmandcr", SmallInteger, nullable=True),
        Column("slozneplat", Text, nullable=True),
        Column("nazevplny", Text, nullable=True),
        UniqueConstraint("datum", "kstrana"),
    ),
    Table(
        "psp_okrsky_hlasy",
        meta,
        Column("datum", Date, nullable=False),
        Column("id_okrsky", Integer, nullable=False),
        Column("typ_form", Integer, nullable=True),
        Column("oprava", Integer, nullable=True),
        Column("chyba", Integer, nullable=True),
        Column("kraj", Integer, nullable=True),
        Column("okres", Integer, nullable=False),
        Column("obec", Integer, nullable=False),
        Column("okrsek", Integer, nullable=False),
        Column("kc_1", Integer, nullable=True),
        Column("kstrana", Integer, nullable=False),
        Column("poc_hlasu", Integer, nullable=False),
        Column("kc_2", Integer, nullable=True),
        Column("kc_3", Integer, nullable=True),
        Column("kc_4", Integer, nullable=True),
        Column("posl_kand", Integer, nullable=True),
        Column("kc_sum", Integer, nullable=True),
        Column("hlasy", array_ish, nullable=False),
        UniqueConstraint("datum", "okres", "obec", "okrsek", "kstrana"),
    ),
    Table(
        "psp_okrsky_prehled",
        meta,
        Column("datum", Date, nullable=False),
        Column("id_okrsky", Integer, nullable=False),
        Column("typ_form", Integer, nullable=True),
        Column("oprava", Integer, nullable=True),
        Column("chyba", Integer, nullable=True),
        Column("kraj", Integer, nullable=True),
        Column("okres", Integer, nullable=False),
        Column("obec", Integer, nullable=False),
        Column("okrsek", Integer, nullable=False),
        Column("kc_1", Integer, nullable=True),
        Column("kc_2", Integer, nullable=True),
        Column("zakrstrana", bitmap_ish(60), nullable=True),
        Column("vol_seznam", Integer, nullable=False),
        Column("vyd_obalky", Integer, nullable=False),
        Column("odevz_obal", Integer, nullable=False),
        Column("pl_hl_celk", Integer, nullable=False),
        UniqueConstraint("datum", "okres", "obec", "okrsek"),
    ),
    Table(
        "psp_nuts",
        meta,
        Column("datum", Date, nullable=False),
        Column("num_nuts", Integer, nullable=False),
        Column("nuts", Text, nullable=True),  # kvuli zahranicnimu "okresu"
        Column("nazev", Text, nullable=False),
        Column("ciselnik_kod", Integer, nullable=True),
        Column("ciselnik_hodnota", Integer, nullable=True),
        UniqueConstraint("datum", "num_nuts"),
    ),
    Table(
        "komunalni_davky",
        meta,
        Column("datum", Date, nullable=False),
        Column("cis_obec", Integer, nullable=False),
        Column("cis_okrsek", Integer, nullable=False),
        Column("kodzastup", Integer, nullable=False),
        Column("cis_obvodu", SmallInteger, nullable=False),
        Column("oznac_typu", Text, nullable=False),
        Column("poradi_zprac", SmallInteger, nullable=False),
        Column("datum_cas_zprac", DateTime, nullable=False),
        Column("opakovane", Boolean, nullable=False),
    ),
    Table(
        "komunalni_vysledky_obce",
        meta,
        Column("datum", Date, nullable=False),
        Column("platnostk", Date, nullable=False),
        Column("okres", SmallInteger, nullable=False),
        Column("kodzastup", Integer, nullable=False),
        Column("nazevzast", Text, nullable=False),
        Column("cobvodu", SmallInteger, nullable=False),
        Column("por_str_hl", SmallInteger, nullable=False),
        Column("ostrana", SmallInteger, nullable=False),
        Column("vstrana", SmallInteger, nullable=False),
        Column("nazevcelk", Text, nullable=False),
        Column("zkratkao30", Text, nullable=False),
        Column("zkratkao8", Text, nullable=True),
        Column("pocstr_slo", SmallInteger, nullable=False),
        Column("slozeni", Text, nullable=False),
        Column("hlasy_str", Integer, nullable=True),
        Column("prochlstr", Numeric(5, 2), nullable=True),
        Column("mand_str", SmallInteger, nullable=True),
        UniqueConstraint(
            "datum", "platnostk", "okres", "kodzastup", "cobvodu", "por_str_hl"
        ),
    ),
    Table(
        "komunalni_kandidati",
        meta,
        Column("datum", Date, nullable=False),
        Column("platnostk", Date, nullable=False),
        Column("okres", SmallInteger, nullable=False),
        Column("kodzastup", Integer, nullable=False),
        Column("cobvodu", SmallInteger, nullable=False),
        Column("por_str_hl", SmallInteger, nullable=False),
        Column("ostrana", SmallInteger, nullable=False),
        Column("porcislo", SmallInteger, nullable=False),
        Column("jmeno", Text, nullable=False),
        Column("prijmeni", Text, nullable=False),
        Column("titulpred", Text, nullable=True),
        Column("titulza", Text, nullable=True),
        Column("vek", SmallInteger, nullable=True),
        Column("povolani", Text, nullable=True),
        Column("bydlisten", Text, nullable=True),
        Column("pstrana", SmallInteger, nullable=True),
        Column("nstrana", SmallInteger, nullable=True),
        Column("platnost", Text, nullable=False),
        Column("pochlasu", Integer, nullable=True),
        Column("preskoceni", String(1), nullable=False),
        Column("pochl_pres", Integer, nullable=True),
        Column("pocprocvse", Numeric(5, 2), nullable=True),
        Column("mandat", Boolean, nullable=True),
        Column("poradimand", SmallInteger, nullable=False),
        Column("poradinahr", SmallInteger, nullable=False),
        UniqueConstraint(
            "datum",
            "platnostk",
            "okres",
            "kodzastup",
            "cobvodu",
            "por_str_hl",
            "porcislo",
        ),
    ),
    Table(
        "komunalni_strany",
        meta,
        Column("datum", Date, nullable=False),
        Column("vstrana", SmallInteger, nullable=False),
        Column("nazevcelk", Text, nullable=False),
        Column("nazev_strv", Text, nullable=False),
        Column("zkratkav30", Text, nullable=False),
        Column("zkratkav8", Text, nullable=True),
        Column("pocstr_slo", Text, nullable=False),
        Column("slozeni", Text, nullable=False),
        Column("zkratka_of", Text, nullable=True),
        Column("typvs", Text, nullable=True),
        UniqueConstraint("datum", "vstrana"),
    ),
    Table(
        "komunalni_obce",
        meta,
        Column("datum", Date, nullable=False),
        Column("platnostk", Date, nullable=False),
        Column("kraj", Text, nullable=False),
        Column("okres", SmallInteger, nullable=False),
        Column("typzastup", SmallInteger, nullable=False),
        Column("druhzastup", SmallInteger, nullable=False),
        Column("kodzastup", Integer, nullable=False),
        Column("nazevzast", Text, nullable=False),
        Column("obec", Integer, nullable=False),
        Column("nazevobce", Text, nullable=False),
        Column("orp", Text, nullable=True),
        Column("cpou", Integer, nullable=False),
        Column("regurad", Integer, nullable=False),
        Column("obvody", SmallInteger, nullable=False),
        Column("cobvodu", SmallInteger, nullable=False),
        Column("mandaty", SmallInteger, nullable=False),
        Column("pocobyv", Integer, nullable=False),
        Column("typduvodu", SmallInteger, nullable=False),
        Column("pocet_vs", SmallInteger, nullable=False),
        Column("stav_obce", SmallInteger, nullable=False),
        # TODO: tady je kotel sloupcu, to je na unique constraint pres vsechny snad
        # UniqueConstraint("datum", "kraj", "okres"),
    ),
    Table(
        "komunalni_okrsky_prehled",
        meta,
        Column("datum", Date, nullable=False),
        Column("platnostk", Date, nullable=False),
        Column("id_okrsky", Integer, nullable=False),
        Column("typ_form", SmallInteger, nullable=True),
        Column("oprava", SmallInteger, nullable=True),
        Column("chyba", SmallInteger, nullable=True),
        Column("kraj", Integer, nullable=False),
        Column("okres", Integer, nullable=False),
        Column("obec", Integer, nullable=False),
        Column("okrsek", Integer, nullable=False),
        Column("kc_1", Integer, nullable=True),
        Column("typzastup", SmallInteger, nullable=False),
        Column("cobvodu", SmallInteger, nullable=False),
        Column("vol_seznam", Integer, nullable=False),
        Column("vyd_obalky", Integer, nullable=False),
        Column("odevz_obal", Integer, nullable=False),
        Column("pl_hl_celk", Integer, nullable=False),
        Column("pocet_vs", SmallInteger, nullable=False),
        Column("poc_vs_hl", SmallInteger, nullable=False),
        Column("kc_2", Integer, nullable=True),
        Column("kodzastup", Integer, nullable=False),
        UniqueConstraint(
            "datum", "platnostk", "okres", "obec", "okrsek", "typzastup", "cobvodu"
        ),
    ),
    Table(
        "komunalni_okrsky_hlasy",
        meta,
        Column("datum", Date, nullable=False),
        Column("platnostk", Date, nullable=False),
        Column("id_okrsky", Integer, nullable=False),
        Column("typ_form", SmallInteger, nullable=True),
        Column("oprava", SmallInteger, nullable=True),
        Column("chyba", SmallInteger, nullable=True),
        Column("kraj", Integer, nullable=False),
        Column("okres", Integer, nullable=False),
        Column("obec", Integer, nullable=False),
        Column("okrsek", Integer, nullable=False),
        Column("kc_1", Integer, nullable=True),
        Column("typzastup", SmallInteger, nullable=False),
        Column("por_str_hl", Integer, nullable=False),
        Column("poc_hlasu", Integer, nullable=False),
        Column("kc_2", Integer, nullable=True),
        Column("posl_kand", SmallInteger, nullable=True),
        Column("kc_sum", Integer, nullable=True),
        Column("hlasy", array_ish, nullable=False),
        UniqueConstraint(
            "datum", "platnostk", "okres", "obec", "okrsek", "typzastup", "por_str_hl"
        ),
    ),
    Table(
        "komunalni_nuts",
        meta,
        Column("datum", Date, nullable=False),
        Column("num_nuts", Integer, nullable=False),
        Column("nuts", Text, nullable=True),  # kvuli zahranicnimu "okresu"
        Column("nazev", Text, nullable=False),
        Column("ciselnik_kod", Integer, nullable=True),
        Column("ciselnik_hodnota", Integer, nullable=True),
        UniqueConstraint("datum", "num_nuts"),
    ),
    Table(
        "kraje_davky",
        meta,
        Column("datum", Date, nullable=False),
        Column("cis_obec", Integer, nullable=False),
        Column("cis_okrsek", Integer, nullable=False),
        Column("poradi_zprac", SmallInteger, nullable=False),
        Column("datum_cas_zprac", DateTime, nullable=False),
        Column("opakovane", Boolean, nullable=False),
    ),
    Table(
        "kraje_kandidati",
        meta,
        Column("datum", Date, nullable=False),
        Column("krzast", SmallInteger, nullable=False),
        Column("kstrana", SmallInteger, nullable=False),
        Column("porcislo", Text, nullable=False),
        Column("jmeno", Text, nullable=False),
        Column("prijmeni", Text, nullable=False),
        Column("titulpred", Text, nullable=True),
        Column("titulza", Text, nullable=True),
        Column("vek", SmallInteger, nullable=True),
        Column("povolani", Text, nullable=True),
        Column("bydlisten", Text, nullable=True),
        Column("bydlistek", Integer, nullable=True),
        Column("pstrana", SmallInteger, nullable=True),
        Column("nstrana", SmallInteger, nullable=True),
        Column("platnost", Text, nullable=False),
        Column("pochlasu", Integer, nullable=True),
        Column("pocproc", Numeric(5, 2), nullable=True),
        Column("pocprocvse", Numeric(5, 2), nullable=True),
        Column("mandat", Boolean, nullable=True),
        Column("poradimand", SmallInteger, nullable=True),
        Column("poradinahr", SmallInteger, nullable=True),
        Column("poradihahr", SmallInteger, nullable=True),
        UniqueConstraint("datum", "krzast", "kstrana", "porcislo"),
    ),
    Table(
        "kraje_strany_cr",
        meta,
        Column("datum", Date, nullable=False),
        Column("kstrana", SmallInteger, nullable=False),
        Column("vstrana", SmallInteger, nullable=False),
        Column("nazevcelk", Text, nullable=False),
        Column("nazev_strk", Text, nullable=False),
        Column("zkratkak30", Text, nullable=False),
        Column("zkratkak8", Text, nullable=False),
        Column("pocstrvko", SmallInteger, nullable=False),
        Column("slozeni", Text, nullable=False),
        Column("stavreg", Text, nullable=False),
        Column("plat_str", Text, nullable=False),
        Column("pocmandcr", SmallInteger, nullable=False),
        Column("nazevplny", Text, nullable=True),
        Column("platnost", Text, nullable=True),
        Column("slozneplat", Text, nullable=True),
        UniqueConstraint("datum", "kstrana"),
    ),
    Table(
        "kraje_strany_navrhujici",
        meta,
        Column("datum", Date, nullable=False),
        Column("nstrana", SmallInteger, nullable=False),
        Column("nazev_strn", Text, nullable=False),
        Column("zkratkan30", Text, nullable=False),
        Column("zkratkan8", Text, nullable=False),
        UniqueConstraint("datum", "nstrana"),
    ),
    Table(
        "kraje_strany_kraje",
        meta,
        Column("datum", Date, nullable=False),
        Column("krzast", SmallInteger, nullable=False),
        Column("kstrana", SmallInteger, nullable=False),
        Column("vstrana", SmallInteger, nullable=False),
        Column("nazevcelk", Text, nullable=False),
        Column("nazev_strk", Text, nullable=False),
        Column("zkratkak30", Text, nullable=False),
        Column("zkratkak8", Text, nullable=False),
        Column("pocstrvko", SmallInteger, nullable=False),
        Column("slozeni", Text, nullable=False),
        Column("stavreg", SmallInteger, nullable=False),
        Column("plat_str", Text, nullable=False),
        Column("platnost", Text, nullable=True),
        Column("slozneplat", Text, nullable=True),
        UniqueConstraint("datum", "krzast", "kstrana"),
    ),
    Table(
        "kraje_obce",
        meta,
        Column("datum", Date, nullable=False),
        Column("kraj", Integer, nullable=False),
        Column("okres", Integer, nullable=False),
        Column("cpou", Integer, nullable=False),
        Column("orp", Integer, nullable=True),
        Column("obec", Integer, nullable=False),
        Column("nazevobce", Text, nullable=False),
        Column("krzast", SmallInteger, nullable=False),
        Column("obec_prez", Integer, nullable=False),
        UniqueConstraint("datum", "kraj", "okres", "obec"),
    ),
    Table(
        "kraje_okrsky_prehled",
        meta,
        Column("datum", Date, nullable=False),
        Column("id_okrsky", SmallInteger, nullable=False),
        Column("typ_form", SmallInteger, nullable=True),
        Column("oprava", SmallInteger, nullable=True),
        Column("chyba", SmallInteger, nullable=True),
        Column("kraj", SmallInteger, nullable=True),
        Column("okres", SmallInteger, nullable=False),
        Column("obec", Integer, nullable=False),
        Column("okrsek", Integer, nullable=False),
        Column("kc_1", Integer, nullable=True),
        Column("vol_seznam", SmallInteger, nullable=False),
        Column("vyd_obalky", SmallInteger, nullable=False),
        Column("odevz_obal", SmallInteger, nullable=False),
        Column("pl_hl_celk", SmallInteger, nullable=False),
        Column("kc_2", Integer, nullable=True),
        Column("zakrstrana", bitmap_ish(99), nullable=True),
        UniqueConstraint("datum", "okres", "obec", "okrsek"),
    ),
    Table(
        "kraje_okrsky_hlasy",
        meta,
        Column("datum", Date, nullable=False),
        Column("id_okrsky", SmallInteger, nullable=False),
        Column("typ_form", SmallInteger, nullable=True),
        Column("oprava", SmallInteger, nullable=True),
        Column("chyba", SmallInteger, nullable=True),
        Column("kraj", SmallInteger, nullable=True),
        Column("okres", SmallInteger, nullable=False),
        Column("obec", Integer, nullable=False),
        Column("okrsek", Integer, nullable=False),
        Column("kc_1", Integer, nullable=True),
        Column("kstrana", SmallInteger, nullable=False),
        Column("poc_hlasu", SmallInteger, nullable=False),
        Column("kc_2", Integer, nullable=True),
        Column("kc_3", Integer, nullable=True),
        Column("kc_4", Integer, nullable=True),
        Column("kc_5", Integer, nullable=True),
        Column("posl_kand", SmallInteger, nullable=True),
        Column("kc_sum", Integer, nullable=True),
        Column("hlasy", array_ish, nullable=False),
        UniqueConstraint("datum", "okres", "obec", "okrsek", "kstrana"),
    ),
    Table(
        "ep_kandidati",
        meta,
        Column("datum", Date, nullable=False),
        Column("estrana", SmallInteger, nullable=False),
        Column("porcislo", SmallInteger, nullable=False),
        Column("jmeno", Text, nullable=False),
        Column("prijmeni", Text, nullable=False),
        Column("titulpred", Text, nullable=True),
        Column("titulza", Text, nullable=True),
        Column("vek", SmallInteger, nullable=True),
        Column("statobcan", Text, nullable=True),
        Column("povolani", Text, nullable=True),
        Column("bydlisten", Text, nullable=True),
        Column("bydlistek", Integer, nullable=True),
        Column("pstrana", SmallInteger, nullable=True),
        Column("nstrana", SmallInteger, nullable=True),
        Column("platnost", Text, nullable=False),
        Column("pochlasu", Integer, nullable=False),
        Column("pocproc", Numeric(5, 2), nullable=False),
        Column("pocprocvse", Numeric(5, 2), nullable=True),
        Column("mandat", Boolean, nullable=False),
        Column("poradimand", SmallInteger, nullable=False),
        Column("poradinahr", SmallInteger, nullable=False),
        UniqueConstraint("datum", "estrana", "porcislo"),
    ),
    Table(
        "ep_strany",
        meta,
        Column("datum", Date, nullable=False),
        Column("estrana", SmallInteger, nullable=False),
        Column("vstrana", SmallInteger, nullable=False),
        Column("nazevcelk", Text, nullable=False),
        Column("nazev_stre", Text, nullable=False),
        Column("zkratkae30", Text, nullable=False),
        Column("zkratkae8", Text, nullable=False),
        Column("pocstrvko", SmallInteger, nullable=False),
        Column("slozeni", Text, nullable=False),
        Column("stavreg", SmallInteger, nullable=False),
        Column("platnost", SmallInteger, nullable=True),
        Column("plat_str", String(1), nullable=False),
        Column("slozneplat", String(40), nullable=True),
        Column("pocmandcr", SmallInteger, nullable=False),
        Column("nazevplny", Text, nullable=True),
        UniqueConstraint("datum", "estrana"),
    ),
    Table(
        "ep_okrsky_prehled",
        meta,
        Column("datum", Date, nullable=False),
        Column("id_okrsky", SmallInteger, nullable=False),
        Column("typ_form", SmallInteger, nullable=True),
        Column("oprava", SmallInteger, nullable=True),
        Column("chyba", SmallInteger, nullable=True),
        Column("kraj", Integer, nullable=True),
        Column("okres", Integer, nullable=False),
        Column("obec", Integer, nullable=False),
        Column("okrsek", Integer, nullable=False),
        Column("kc_1", Integer, nullable=True),
        Column("kc_2", Integer, nullable=True),
        Column("zakrstrana", bitmap_ish(60), nullable=True),
        Column("vol_seznam", SmallInteger, nullable=False),
        Column("vyd_obalky", SmallInteger, nullable=False),
        Column("odevz_obal", SmallInteger, nullable=False),
        Column("pl_hl_celk", SmallInteger, nullable=False),
        UniqueConstraint("datum", "okres", "obec", "okrsek"),
    ),
    Table(
        "ep_okrsky_hlasy",
        meta,
        Column("datum", Date, nullable=False),
        Column("id_okrsky", SmallInteger, nullable=False),
        Column("typ_form", SmallInteger, nullable=True),
        Column("oprava", SmallInteger, nullable=True),
        Column("chyba", SmallInteger, nullable=True),
        Column("kraj", Integer, nullable=True),
        Column("okres", Integer, nullable=False),
        Column("obec", Integer, nullable=False),
        Column("okrsek", Integer, nullable=False),
        Column("kc_1", Integer, nullable=True),
        Column("estrana", SmallInteger, nullable=False),
        Column("poc_hlasu", Integer, nullable=False),
        Column("kc_2", Integer, nullable=True),
        Column("kc_3", Integer, nullable=True),
        Column("kc_4", Integer, nullable=True),
        Column("posl_kand", SmallInteger, nullable=True),
        Column("kc_sum", Integer, nullable=True),
        Column("hlasy", array_ish, nullable=False),
        UniqueConstraint("datum", "okres", "obec", "okrsek", "estrana"),
    ),
    Table(
        "senat_davky",
        meta,
        Column("datum", Date, nullable=False),
        Column("kolo", SmallInteger, nullable=False),
        Column("cis_obec", Integer, nullable=False),
        Column("cis_okrsek", Integer, nullable=False),
        Column("cis_obvod", SmallInteger, nullable=False),
        Column("poradi_zprac", SmallInteger, nullable=False),
        Column("datum_cas_zprac", DateTime, nullable=False),
        Column("opakovane", Boolean, nullable=False),
    ),
    Table(
        "senat_kandidati",
        meta,
        Column("datum", Date, nullable=False),
        Column("obvod", SmallInteger, nullable=False),
        Column("ckand", SmallInteger, nullable=False),
        Column("vstrana", SmallInteger, nullable=False),
        Column("jmeno", Text, nullable=True),
        Column("prijmeni", Text, nullable=True),
        Column("titulpred", Text, nullable=True),
        Column("titulza", Text, nullable=True),
        Column("vek", SmallInteger, nullable=True),
        Column("povolani", Text, nullable=True),
        Column("bydlisten", Text, nullable=True),
        Column("bydlistek", Integer, nullable=True),
        Column("pstrana", SmallInteger, nullable=True),
        Column("nstrana", SmallInteger, nullable=True),
        Column("platnost", Text, nullable=False),
        Column("hlasy_k1", Integer, nullable=False),
        Column("proc_k1", Numeric(5, 2), nullable=False),
        Column("uriz_pr_k1", Float, nullable=False),
        Column("zvolen_k1", SmallInteger, nullable=False),
        Column("los_k1", SmallInteger, nullable=False),
        Column("hlasy_k2", Integer, nullable=False),
        Column("proc_k2", Numeric(5, 2), nullable=False),
        Column("uriz_pr_k2", Float, nullable=False),
        Column("zvolen_k2", SmallInteger, nullable=False),
        Column("los_k2", SmallInteger, nullable=False),
        Column("nazev_vs", Text, nullable=True),
        UniqueConstraint("datum", "obvod", "ckand"),
    ),
    Table(
        "senat_obvody",
        meta,
        # nullable, protoze u ciselniku povolime NULL pro globalni ciselniky
        Column("datum", Date, nullable=True),
        Column("obvod", SmallInteger, nullable=False),
        Column("nazev_obv", Text, nullable=False),
        Column("okres", Text, nullable=False),
        Column("prvni_vo", Text, nullable=False),
        Column("platnost", Text, nullable=False),
        UniqueConstraint("datum", "obvod"),
    ),
    Table(
        "senat_okrsky",
        meta,
        Column("datum", Date, nullable=False),
        Column("obec", Integer, nullable=False),
        Column("okrsek", Integer, nullable=False),
        Column("obvod", Integer, nullable=False),
        Column("kolo", SmallInteger, nullable=False),
        Column("vol_seznam", Integer, nullable=False),
        Column("vyd_obalky", Integer, nullable=False),
        Column("odevz_obal", Integer, nullable=False),
        Column("pl_hl_celk", Integer, nullable=False),
        # TODO: k cemu ten index?
        Column("hlasy", array_ish, nullable=False, index=True),
        UniqueConstraint("datum", "obec", "okrsek", "obvod", "kolo"),
    ),
]


if __name__ == "__main__":
    from sqlalchemy.dialects import postgresql
    from sqlalchemy.schema import CreateTable

    for table in schema:
        print(f"-- {table.name} as created in Postgres")

        print(CreateTable(table).compile(dialect=postgresql.dialect()))


# TODO: co s views?


# create view volby.v_kandidati AS (
#     SELECT
#         'senat' AS volby,
#         obvod || ' - ' || nazev_obv as obvod,
#         datum,
#         jmeno || ' ' || prijmeni AS jmeno,
#         nazev_vs AS strana,
#         vek,
#         povolani,
#         (zvolen_k1 = 1) OR (zvolen_k2 = 1) AS zvolen
#     FROM
#         volby.senat_kandidati
#         INNER JOIN volby.senat_obvody USING(datum, obvod)

#     UNION ALL

#     SELECT
#         'ep' AS volby,
#         NULL AS obvod,
#         datum,
#         jmeno || ' ' || prijmeni as jmeno,
#         nazevcelk AS strana,
#         vek,
#         povolani,
#         mandat AS zvolen
#     FROM
#         volby.ep_kandidati
#         INNER JOIN volby.ep_strany USING (datum, estrana)

#     UNION ALL

#     SELECT
#         'obce' AS volby,
#         NULL AS obvod, -- TODO
#         kn.datum,
#         jmeno || ' ' || prijmeni AS jmeno,
#         nazevcelk AS strana,
#         vek,
#         povolani,
#         mandat AS zvolen
#     FROM
#         volby.komunalni_kandidati kn
#         INNER JOIN volby.komunalni_strany ks ON kn.nstrana = ks.vstrana
#             AND kn.datum = ks.datum

#     UNION ALL

#     SELECT
#         'kraje' AS volby,
#         NULL AS obvod, -- TODO
#         kn.datum,
#         jmeno || ' ' || prijmeni AS jmeno,
#         nazevcelk AS strana,
#         vek,
#         povolani,
#         mandat AS zvolen
#     FROM
#         volby.kraje_kandidati kn
#         INNER JOIN volby.kraje_strany_cr ks ON kn.nstrana = ks.vstrana
#             AND kn.datum = ks.datum

#     UNION ALL

#     SELECT
#         'psp' AS volby,
#         NULL AS obvod, -- TODO
#         kn.datum,
#         jmeno || ' ' || prijmeni AS jmeno,
#         nazevcelk AS strana,
#         vek,
#         povolani,
#         mandat AS zvolen
#     FROM
#         volby.psp_kandidati kn
#         INNER JOIN volby.psp_strany ks ON ks.datum = kn.datum
#             AND ks.vstrana = kn.nstrana

#     UNION ALL

#     SELECT
#         'prezident' AS volby,
#         NULL AS obvod,
#         kn.datum,
#         jmeno || ' ' || prijmeni AS jmeno,
#         ps.nazev_strn as strana,
#         vek,
#         povolani,
#         (zvolen_k1 = 1) OR (zvolen_k2 = 1) AS zvolen
#     FROM
#         volby.prezident_kandidati kn
#         INNER JOIN volby.prezident_strany ps ON kn.nstrana = ps.nstrana
#         AND kn.datum = ps.datum
# );
