"""tables module gestions relations entreprises suite

Revision ID: e97b2a10f86c
Revises: af05f03b81be
Create Date: 2022-04-19 17:39:08.197835

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "e97b2a10f86c"
down_revision = "af05f03b81be"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "are_correspondants",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("entreprise_id", sa.Integer(), nullable=True),
        sa.Column("nom", sa.Text(), nullable=True),
        sa.Column("prenom", sa.Text(), nullable=True),
        sa.Column("telephone", sa.Text(), nullable=True),
        sa.Column("mail", sa.Text(), nullable=True),
        sa.Column("poste", sa.Text(), nullable=True),
        sa.Column("service", sa.Text(), nullable=True),
        sa.ForeignKeyConstraint(
            ["entreprise_id"], ["are_entreprises.id"], ondelete="cascade"
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "are_stages_apprentissages",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("entreprise_id", sa.Integer(), nullable=True),
        sa.Column("etudid", sa.Integer(), nullable=True),
        sa.Column("type_offre", sa.Text(), nullable=True),
        sa.Column("date_debut", sa.Date(), nullable=True),
        sa.Column("date_fin", sa.Date(), nullable=True),
        sa.Column("formation_text", sa.Text(), nullable=True),
        sa.Column("formation_scodoc", sa.Integer(), nullable=True),
        sa.Column("notes", sa.Text(), nullable=True),
        sa.ForeignKeyConstraint(
            ["entreprise_id"], ["are_entreprises.id"], ondelete="cascade"
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.drop_table("are_etudiants")
    op.add_column(
        "are_contacts", sa.Column("date", sa.DateTime(timezone=True), nullable=True)
    )
    op.add_column("are_contacts", sa.Column("user", sa.Integer(), nullable=True))
    op.add_column("are_contacts", sa.Column("entreprise", sa.Integer(), nullable=True))
    op.add_column("are_contacts", sa.Column("notes", sa.Text(), nullable=True))
    op.drop_constraint(
        "are_contacts_entreprise_id_fkey", "are_contacts", type_="foreignkey"
    )
    op.create_foreign_key(
        None,
        "are_contacts",
        "are_entreprises",
        ["entreprise"],
        ["id"],
        ondelete="cascade",
    )
    op.create_foreign_key(
        None, "are_contacts", "user", ["user"], ["id"], ondelete="cascade"
    )
    op.drop_column("are_contacts", "nom")
    op.drop_column("are_contacts", "telephone")
    op.drop_column("are_contacts", "service")
    op.drop_column("are_contacts", "entreprise_id")
    op.drop_column("are_contacts", "mail")
    op.drop_column("are_contacts", "poste")
    op.drop_column("are_contacts", "prenom")
    op.add_column(
        "are_offres", sa.Column("correspondant_id", sa.Integer(), nullable=True)
    )
    op.create_foreign_key(
        None,
        "are_offres",
        "are_correspondants",
        ["correspondant_id"],
        ["id"],
        ondelete="cascade",
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(
        "are_offres_correspondant_id_fkey", "are_offres", type_="foreignkey"
    )
    op.drop_column("are_offres", "correspondant_id")
    op.add_column(
        "are_contacts",
        sa.Column("prenom", sa.TEXT(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "are_contacts",
        sa.Column("poste", sa.TEXT(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "are_contacts", sa.Column("mail", sa.TEXT(), autoincrement=False, nullable=True)
    )
    op.add_column(
        "are_contacts",
        sa.Column("entreprise_id", sa.INTEGER(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "are_contacts",
        sa.Column("service", sa.TEXT(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "are_contacts",
        sa.Column("telephone", sa.TEXT(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "are_contacts", sa.Column("nom", sa.TEXT(), autoincrement=False, nullable=True)
    )
    op.drop_constraint(
        "are_contacts_entreprise_fkey", "are_contacts", type_="foreignkey"
    )
    op.drop_constraint("are_contacts_user_fkey", "are_contacts", type_="foreignkey")
    op.create_foreign_key(
        "are_contacts_entreprise_id_fkey",
        "are_contacts",
        "are_entreprises",
        ["entreprise_id"],
        ["id"],
        ondelete="CASCADE",
    )
    op.drop_column("are_contacts", "notes")
    op.drop_column("are_contacts", "entreprise")
    op.drop_column("are_contacts", "user")
    op.drop_column("are_contacts", "date")
    op.create_table(
        "are_etudiants",
        sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
        sa.Column("entreprise_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.Column("etudid", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.Column("type_offre", sa.TEXT(), autoincrement=False, nullable=True),
        sa.Column("date_debut", sa.DATE(), autoincrement=False, nullable=True),
        sa.Column("date_fin", sa.DATE(), autoincrement=False, nullable=True),
        sa.Column("formation_text", sa.TEXT(), autoincrement=False, nullable=True),
        sa.Column("formation_scodoc", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(
            ["entreprise_id"],
            ["are_entreprises.id"],
            name="are_etudiants_entreprise_id_fkey",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name="are_etudiants_pkey"),
    )
    op.drop_table("are_stages_apprentissages")
    op.drop_table("are_correspondants")
    # ### end Alembic commands ###