"""Ajoute les profils horaires et le suivi annuel du temps."""
from alembic import op
import sqlalchemy as sa

revision = "f8b2c3d4e5f6"
down_revision = ("a4b8c2d6e0f1", "f1a5b6c7d8e9", "f7a1b2c3d4e5")
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        "work_schedule_templates",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=False),
        sa.Column("name", sa.String(80), nullable=False),
        sa.Column("start_time", sa.Time(), nullable=False),
        sa.Column("end_time", sa.Time(), nullable=False),
        sa.Column("lunch_start", sa.Time()),
        sa.Column("lunch_end", sa.Time()),
        sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.true()),
    )
    op.create_index("ix_work_schedule_templates_user_id", "work_schedule_templates", ["user_id"])
    op.add_column("work_schedules", sa.Column("template_id", sa.Integer(), nullable=True))
    op.create_foreign_key("fk_work_schedules_template_id", "work_schedules", "work_schedule_templates", ["template_id"], ["id"])

    op.create_table(
        "annual_time_configs",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=False),
        sa.Column("year", sa.Integer(), nullable=False),
        sa.Column("target_minutes", sa.Integer(), nullable=False, server_default="96420"),
        sa.Column("hardship_deduction_minutes", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("notes", sa.Text()),
        sa.UniqueConstraint("user_id", "year", name="uq_annual_time_user_year"),
    )
    op.create_index("ix_annual_time_configs_user_id", "annual_time_configs", ["user_id"])

    op.create_table(
        "work_time_entries",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=False),
        sa.Column("work_date", sa.Date(), nullable=False),
        sa.Column("planned_minutes", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("actual_minutes", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("entry_type", sa.String(30), nullable=False, server_default="travail"),
        sa.Column("note", sa.Text()),
        sa.Column("created_at", sa.DateTime()),
        sa.Column("updated_at", sa.DateTime()),
        sa.UniqueConstraint("user_id", "work_date", name="uq_work_time_user_date"),
    )
    op.create_index("ix_work_time_entries_user_id", "work_time_entries", ["user_id"])
    op.create_index("ix_work_time_entries_work_date", "work_time_entries", ["work_date"])


def downgrade():
    op.drop_index("ix_work_time_entries_work_date", table_name="work_time_entries")
    op.drop_index("ix_work_time_entries_user_id", table_name="work_time_entries")
    op.drop_table("work_time_entries")
    op.drop_index("ix_annual_time_configs_user_id", table_name="annual_time_configs")
    op.drop_table("annual_time_configs")
    op.drop_constraint("fk_work_schedules_template_id", "work_schedules", type_="foreignkey")
    op.drop_column("work_schedules", "template_id")
    op.drop_index("ix_work_schedule_templates_user_id", table_name="work_schedule_templates")
    op.drop_table("work_schedule_templates")
