"""projects table + time_entries FK

Revision ID: 0013_projects
Revises: 0012_caldav_template_and_kuerzel
Create Date: 2026-03-28
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID

revision = "0013_projects"
down_revision = "0012_caldav_template_and_kuerzel"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Projekte-Tabelle
    op.create_table(
        "projects",
        sa.Column("id", UUID(as_uuid=True), primary_key=True),
        sa.Column("company_id", UUID(as_uuid=True),
                  sa.ForeignKey("companies.id", ondelete="CASCADE"), nullable=False, index=True),
        sa.Column("name", sa.String(100), nullable=False),
        sa.Column("description", sa.Text, nullable=True),
        sa.Column("color", sa.String(7), nullable=False, server_default="#3B82F6"),
        sa.Column("budget_hours", sa.Numeric(8, 2), nullable=True),
        sa.Column("is_active", sa.Boolean, nullable=False, server_default="true"),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
    )

    # FK von time_entries.project_id → projects.id
    op.create_foreign_key(
        "fk_time_entries_project_id",
        "time_entries", "projects",
        ["project_id"], ["id"],
        ondelete="SET NULL",
    )


def downgrade() -> None:
    op.drop_constraint("fk_time_entries_project_id", "time_entries", type_="foreignkey")
    op.drop_table("projects")
