defmodule SpazioSolazzo.Repo.Migrations.CreateBaseResources do
  @moduledoc """
  Updates resources based on their most recent snapshots.

  This file was autogenerated with `mix ash_postgres.generate_migrations`
  """

  use Ecto.Migration

  def up do
    create table(:users, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :email, :citext, null: false
      add :name, :text, null: false
      add :phone_number, :text
      add :role, :text, null: false, default: "customer"
    end

    create unique_index(:users, [:email], name: "users_unique_email_index")

    create table(:tokens, primary_key: false) do
      add :jti, :text, null: false, primary_key: true
      add :subject, :text, null: false
      add :expires_at, :utc_datetime, null: false
      add :purpose, :text, null: false
      add :extra_data, :map

      add :created_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")
    end

    create table(:time_slot_templates, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :start_time, :time, null: false
      add :end_time, :time, null: false
      add :day_of_week, :text, null: false
      add :space_id, :uuid, null: false
    end

    create table(:spaces, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
    end

    alter table(:time_slot_templates) do
      modify :space_id,
             references(:spaces,
               column: :id,
               name: "time_slot_templates_space_id_fkey",
               type: :uuid,
               prefix: "public"
             )
    end

    alter table(:spaces) do
      add :name, :text, null: false
      add :description, :text, null: false
      add :slug, :text, null: false
      add :capacity, :bigint, null: false
    end

    create unique_index(:spaces, [:name], name: "spaces_unique_name_index")

    create unique_index(:spaces, [:slug], name: "spaces_unique_slug_index")

    create table(:bookings, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
      add :start_datetime, :utc_datetime, null: false
      add :end_datetime, :utc_datetime, null: false
      add :date, :date, null: false
      add :customer_name, :text, null: false
      add :customer_email, :text, null: false
      add :start_time, :time, null: false
      add :end_time, :time, null: false
      add :customer_phone, :text
      add :customer_comment, :text
      add :cancellation_reason, :text
      add :rejection_reason, :text
      add :state, :text, null: false, default: "requested"

      add :inserted_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :updated_at, :utc_datetime_usec,
        null: false,
        default: fragment("(now() AT TIME ZONE 'utc')")

      add :space_id,
          references(:spaces,
            column: :id,
            name: "bookings_space_id_fkey",
            type: :uuid,
            prefix: "public"
          ), null: false

      add :user_id,
          references(:users,
            column: :id,
            name: "bookings_user_id_fkey",
            type: :uuid,
            prefix: "public",
            on_delete: :nilify_all
          )
    end

    create index(:bookings, [:user_id])
  end

  def down do
    drop_if_exists index(:bookings, [:user_id])

    drop constraint(:bookings, "bookings_space_id_fkey")

    drop constraint(:bookings, "bookings_user_id_fkey")

    drop table(:bookings)

    drop_if_exists unique_index(:spaces, [:slug], name: "spaces_unique_slug_index")

    drop_if_exists unique_index(:spaces, [:name], name: "spaces_unique_name_index")

    alter table(:spaces) do
      remove :capacity
      remove :slug
      remove :description
      remove :name
    end

    drop constraint(:time_slot_templates, "time_slot_templates_space_id_fkey")

    alter table(:time_slot_templates) do
      modify :space_id, :uuid
    end

    drop table(:spaces)

    drop table(:time_slot_templates)

    drop table(:tokens)

    drop_if_exists unique_index(:users, [:email], name: "users_unique_email_index")

    drop table(:users)
  end
end
