-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "public";

-- CreateTable
CREATE TABLE "teams_app_team" (
    "id" SERIAL NOT NULL,
    "uuid" TEXT NOT NULL,
    "name" VARCHAR(200) NOT NULL,
    "team_type" VARCHAR(20) NOT NULL DEFAULT 'BUYER',
    "logto_org_id" VARCHAR(100),
    "owner_id" INTEGER,
    "selected_location_type" VARCHAR(20),
    "selected_location_uuid" VARCHAR(100),
    "selected_location_name" VARCHAR(255),
    "selected_location_lat" DOUBLE PRECISION,
    "selected_location_lon" DOUBLE PRECISION,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "teams_app_team_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "auth_user" (
    "id" SERIAL NOT NULL,
    "password" VARCHAR(128) NOT NULL DEFAULT '',
    "username" VARCHAR(150) NOT NULL,
    "first_name" VARCHAR(150) NOT NULL DEFAULT '',
    "last_name" VARCHAR(150) NOT NULL DEFAULT '',
    "email" VARCHAR(254) NOT NULL DEFAULT '',
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "date_joined" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "is_superuser" BOOLEAN NOT NULL DEFAULT false,
    "is_staff" BOOLEAN NOT NULL DEFAULT false,
    "last_login" TIMESTAMP(3),

    CONSTRAINT "auth_user_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "teams_app_userprofile" (
    "id" SERIAL NOT NULL,
    "user_id" INTEGER NOT NULL,
    "logto_id" VARCHAR(255) NOT NULL,
    "avatar_id" VARCHAR(255),
    "phone" VARCHAR(50) NOT NULL DEFAULT '',
    "active_team_id" INTEGER,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "teams_app_userprofile_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "teams_app_teammember" (
    "id" SERIAL NOT NULL,
    "uuid" TEXT NOT NULL,
    "team_id" INTEGER NOT NULL,
    "user_id" INTEGER,
    "role" VARCHAR(20) NOT NULL DEFAULT 'MEMBER',
    "joined_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "teams_app_teammember_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "teams_app_teaminvitation" (
    "id" SERIAL NOT NULL,
    "uuid" TEXT NOT NULL,
    "team_id" INTEGER NOT NULL,
    "email" VARCHAR(254) NOT NULL,
    "role" VARCHAR(20) NOT NULL DEFAULT 'MEMBER',
    "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "invited_by" VARCHAR(255) NOT NULL DEFAULT '',
    "expires_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "teams_app_teaminvitation_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "teams_app_teaminvitationtoken" (
    "id" SERIAL NOT NULL,
    "uuid" TEXT NOT NULL,
    "invitation_id" INTEGER NOT NULL,
    "token_hash" VARCHAR(255) NOT NULL,
    "workflow_status" VARCHAR(20) NOT NULL DEFAULT 'pending',
    "expires_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "teams_app_teaminvitationtoken_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "teams_app_teamaddress" (
    "id" SERIAL NOT NULL,
    "uuid" TEXT NOT NULL,
    "team_id" INTEGER NOT NULL,
    "name" VARCHAR(255) NOT NULL,
    "address" TEXT NOT NULL,
    "latitude" DOUBLE PRECISION,
    "longitude" DOUBLE PRECISION,
    "country_code" VARCHAR(10) NOT NULL DEFAULT '',
    "is_default" BOOLEAN NOT NULL DEFAULT false,
    "status" VARCHAR(20) NOT NULL DEFAULT 'pending',
    "processed_at" TIMESTAMP(3),
    "error_message" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "teams_app_teamaddress_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "teams_app_team_uuid_key" ON "teams_app_team"("uuid");

-- CreateIndex
CREATE UNIQUE INDEX "auth_user_username_key" ON "auth_user"("username");

-- CreateIndex
CREATE UNIQUE INDEX "teams_app_userprofile_user_id_key" ON "teams_app_userprofile"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "teams_app_userprofile_logto_id_key" ON "teams_app_userprofile"("logto_id");

-- CreateIndex
CREATE UNIQUE INDEX "teams_app_teammember_uuid_key" ON "teams_app_teammember"("uuid");

-- CreateIndex
CREATE UNIQUE INDEX "teams_app_teammember_team_id_user_id_key" ON "teams_app_teammember"("team_id", "user_id");

-- CreateIndex
CREATE UNIQUE INDEX "teams_app_teaminvitation_uuid_key" ON "teams_app_teaminvitation"("uuid");

-- CreateIndex
CREATE UNIQUE INDEX "teams_app_teaminvitation_team_id_email_key" ON "teams_app_teaminvitation"("team_id", "email");

-- CreateIndex
CREATE UNIQUE INDEX "teams_app_teaminvitationtoken_uuid_key" ON "teams_app_teaminvitationtoken"("uuid");

-- CreateIndex
CREATE UNIQUE INDEX "teams_app_teaminvitationtoken_token_hash_key" ON "teams_app_teaminvitationtoken"("token_hash");

-- CreateIndex
CREATE UNIQUE INDEX "teams_app_teamaddress_uuid_key" ON "teams_app_teamaddress"("uuid");

-- AddForeignKey
ALTER TABLE "teams_app_team" ADD CONSTRAINT "teams_app_team_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "auth_user"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "teams_app_userprofile" ADD CONSTRAINT "teams_app_userprofile_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth_user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "teams_app_userprofile" ADD CONSTRAINT "teams_app_userprofile_active_team_id_fkey" FOREIGN KEY ("active_team_id") REFERENCES "teams_app_team"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "teams_app_teammember" ADD CONSTRAINT "teams_app_teammember_team_id_fkey" FOREIGN KEY ("team_id") REFERENCES "teams_app_team"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "teams_app_teammember" ADD CONSTRAINT "teams_app_teammember_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "auth_user"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "teams_app_teaminvitation" ADD CONSTRAINT "teams_app_teaminvitation_team_id_fkey" FOREIGN KEY ("team_id") REFERENCES "teams_app_team"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "teams_app_teaminvitationtoken" ADD CONSTRAINT "teams_app_teaminvitationtoken_invitation_id_fkey" FOREIGN KEY ("invitation_id") REFERENCES "teams_app_teaminvitation"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "teams_app_teamaddress" ADD CONSTRAINT "teams_app_teamaddress_team_id_fkey" FOREIGN KEY ("team_id") REFERENCES "teams_app_team"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

