from __future__ import annotations

from sqlalchemy import inspect, text
from sqlalchemy.orm import Session

EMPLOYEE_SCHEMA_COLUMNS: dict[str, str] = {
    "password_hash": "ALTER TABLE employees ADD COLUMN password_hash VARCHAR(255)",
    "compliance_score": "ALTER TABLE employees ADD COLUMN compliance_score INTEGER DEFAULT 100 NOT NULL",
    "bank_name": "ALTER TABLE employees ADD COLUMN bank_name VARCHAR(120)",
    "bank_account_no": "ALTER TABLE employees ADD COLUMN bank_account_no VARCHAR(80)",
    "bank_account_name": "ALTER TABLE employees ADD COLUMN bank_account_name VARCHAR(100)",
}


def ensure_employee_schema(db: Session) -> None:
    bind = db.get_bind()
    inspector = inspect(bind)
    if "employees" not in inspector.get_table_names():
        return

    column_names = {column["name"] for column in inspector.get_columns("employees")}
    for column_name, ddl in EMPLOYEE_SCHEMA_COLUMNS.items():
        if column_name not in column_names:
            db.execute(text(ddl))
    db.flush()
