"""create customers

Revision ID: 9c1d8a2f6b44
Revises: 7b2c9f4a0d31
Create Date: 2026-07-02 14:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision: str = "9c1d8a2f6b44"
down_revision: Union[str, Sequence[str], None] = "7b2c9f4a0d31"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "customers",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("customer_number", sa.String(length=50), nullable=False),
        sa.Column("company_name", sa.String(length=255), nullable=False),
        sa.Column("legal_name", sa.String(length=255), server_default="", nullable=False),
        sa.Column("customer_type", sa.String(length=50), nullable=False),
        sa.Column("status", sa.String(length=50), nullable=False),
        sa.Column("industry", sa.String(length=120), server_default="", nullable=False),
        sa.Column("website", sa.String(length=255), server_default="", nullable=False),
        sa.Column("email", sa.String(length=255), server_default="", nullable=False),
        sa.Column("phone", sa.String(length=80), server_default="", nullable=False),
        sa.Column("tax_number", sa.String(length=120), server_default="", nullable=False),
        sa.Column("vat_id", sa.String(length=120), server_default="", nullable=False),
        sa.Column("notes", sa.Text(), server_default="", nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_customers_company_name"), "customers", ["company_name"], unique=False)
    op.create_index(op.f("ix_customers_customer_number"), "customers", ["customer_number"], unique=True)
    op.create_index(op.f("ix_customers_customer_type"), "customers", ["customer_type"], unique=False)
    op.create_index(op.f("ix_customers_status"), "customers", ["status"], unique=False)

    op.create_table(
        "customer_addresses",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("customer_id", sa.Integer(), nullable=False),
        sa.Column("type", sa.String(length=50), nullable=False),
        sa.Column("street", sa.String(length=255), server_default="", nullable=False),
        sa.Column("postal_code", sa.String(length=30), server_default="", nullable=False),
        sa.Column("city", sa.String(length=120), server_default="", nullable=False),
        sa.Column("state", sa.String(length=120), server_default="", nullable=False),
        sa.Column("country", sa.String(length=120), server_default="Deutschland", nullable=False),
        sa.Column("is_primary", sa.Boolean(), server_default=sa.false(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.ForeignKeyConstraint(["customer_id"], ["customers.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_customer_addresses_customer_id"), "customer_addresses", ["customer_id"], unique=False)
    op.create_index(op.f("ix_customer_addresses_type"), "customer_addresses", ["type"], unique=False)

    op.create_table(
        "customer_contacts",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("customer_id", sa.Integer(), nullable=False),
        sa.Column("first_name", sa.String(length=100), server_default="", nullable=False),
        sa.Column("last_name", sa.String(length=100), server_default="", nullable=False),
        sa.Column("position", sa.String(length=120), server_default="", nullable=False),
        sa.Column("email", sa.String(length=255), server_default="", nullable=False),
        sa.Column("phone", sa.String(length=80), server_default="", nullable=False),
        sa.Column("mobile", sa.String(length=80), server_default="", nullable=False),
        sa.Column("is_primary", sa.Boolean(), server_default=sa.false(), nullable=False),
        sa.Column("notes", sa.Text(), server_default="", nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.ForeignKeyConstraint(["customer_id"], ["customers.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_customer_contacts_customer_id"), "customer_contacts", ["customer_id"], unique=False)


def downgrade() -> None:
    op.drop_index(op.f("ix_customer_contacts_customer_id"), table_name="customer_contacts")
    op.drop_table("customer_contacts")
    op.drop_index(op.f("ix_customer_addresses_type"), table_name="customer_addresses")
    op.drop_index(op.f("ix_customer_addresses_customer_id"), table_name="customer_addresses")
    op.drop_table("customer_addresses")
    op.drop_index(op.f("ix_customers_status"), table_name="customers")
    op.drop_index(op.f("ix_customers_customer_type"), table_name="customers")
    op.drop_index(op.f("ix_customers_customer_number"), table_name="customers")
    op.drop_index(op.f("ix_customers_company_name"), table_name="customers")
    op.drop_table("customers")
