"""
ねんきんポータル DB テーブル作成マイグレーション
"""
import os
from psycopg import connect


def create_nenkin_tables():
    conn = connect(
        host=os.getenv("DB_HOST"),
        port=int(os.getenv("DB_PORT", 5432)),
        dbname=os.getenv("DB_NAME"),
        user=os.getenv("DB_USER"),
        password=os.getenv("DB_PASSWORD"),
    )

    with conn.cursor() as cur:
        # ──────────────────────────────────
        # 文書ヘッダーマスター（全種別共通）
        # ──────────────────────────────────
        cur.execute("""
            CREATE TABLE IF NOT EXISTS nenkin_documents (
                id                SERIAL PRIMARY KEY,
                document_no       VARCHAR(50) UNIQUE NOT NULL,
                document_type     VARCHAR(10) NOT NULL,
                document_type_name VARCHAR(100),
                zip_filename      VARCHAR(255),
                office_name       VARCHAR(200),
                office_number     VARCHAR(20),
                office_kigo       VARCHAR(50),
                nenkin_jimusho    VARCHAR(100),
                purpose_year      INTEGER,
                purpose_month     INTEGER,
                issue_date        DATE,
                uploaded_at       TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )
        """)

        # ──────────────────────────────────
        # 社会保険料額情報 (SHAKAI)
        # ──────────────────────────────────
        cur.execute("""
            CREATE TABLE IF NOT EXISTS nenkin_shakai_hokenryo (
                id                       SERIAL PRIMARY KEY,
                document_id              INTEGER REFERENCES nenkin_documents(id) ON DELETE CASCADE,
                kenkou_hokenryo          NUMERIC,
                kousei_nenkin_hokenryo   NUMERIC,
                kodomo_kyoshutsukin      NUMERIC,
                total                    NUMERIC,
                nofu_kigen_date          DATE,
                office_postcode          VARCHAR(10),
                office_address           VARCHAR(300)
            )
        """)

        # ──────────────────────────────────
        # 保険料納入告知額・領収済額通知書 (NOUNYU)
        # ──────────────────────────────────
        cur.execute("""
            CREATE TABLE IF NOT EXISTS nenkin_nounyu_tsuchi (
                id                    SERIAL PRIMARY KEY,
                document_id           INTEGER REFERENCES nenkin_documents(id) ON DELETE CASCADE,
                kokuchi_kenkou        NUMERIC,
                kokuchi_kounen        NUMERIC,
                kokuchi_kodomo        NUMERIC,
                kokuchi_total         NUMERIC,
                kokuchi_nofu_kigen    DATE,
                ryoshu_kenkou         NUMERIC,
                ryoshu_kounen         NUMERIC,
                ryoshu_kodomo         NUMERIC,
                ryoshu_total          NUMERIC,
                ryoshu_date           DATE,
                ryoshu_nofu_year      INTEGER,
                ryoshu_nofu_month     INTEGER,
                office_postcode       VARCHAR(10),
                office_address        VARCHAR(300),
                office_name_line1     VARCHAR(200),
                office_name_line2     VARCHAR(200)
            )
        """)

        # ──────────────────────────────────
        # 賞与保険料算出内訳書 ヘッダー (SHOYO)
        # ──────────────────────────────────
        cur.execute("""
            CREATE TABLE IF NOT EXISTS nenkin_shoyo_header (
                id                      SERIAL PRIMARY KEY,
                document_id             INTEGER REFERENCES nenkin_documents(id) ON DELETE CASCADE,
                jin_in_num              INTEGER,
                menjyo_hokenryo_ritsu   VARCHAR(50),
                page_num                INTEGER
            )
        """)

        # ──────────────────────────────────
        # 賞与保険料算出内訳書 明細 (SHOYO)
        # ──────────────────────────────────
        cur.execute("""
            CREATE TABLE IF NOT EXISTS nenkin_shoyo_detail (
                id                    SERIAL PRIMARY KEY,
                document_id           INTEGER REFERENCES nenkin_documents(id) ON DELETE CASCADE,
                header_id             INTEGER REFERENCES nenkin_shoyo_header(id) ON DELETE CASCADE,
                seiri_num             VARCHAR(20),
                shimei                VARCHAR(100),
                shori_ymd             VARCHAR(20),
                hassei_ymd            VARCHAR(20),
                hyojun_shoyo_kenpo    NUMERIC,
                hyojun_shoyo_kounen   NUMERIC,
                kenpo_hongetsu        NUMERIC,
                kenpo_zengetsu        NUMERIC,
                kounen_hongetsu       NUMERIC,
                kounen_zengetsu       NUMERIC,
                page_num              INTEGER
            )
        """)

    conn.commit()
    conn.close()
    print("✓ ねんきんポータル テーブル作成完了")
