-- CreateEnum
CREATE TYPE "OwnerModelName" AS ENUM ('Owner');

-- DropForeignKey
ALTER TABLE "Address" DROP CONSTRAINT "Address_userId_fkey";

-- DropForeignKey
ALTER TABLE "FinancialAccountAccess" DROP CONSTRAINT "FinancialAccountAccess_userId_fkey";

-- AlterTable
ALTER TABLE "Address" ADD COLUMN     "organizationId" UUID,
ALTER COLUMN "userId" DROP NOT NULL;

-- AlterTable
ALTER TABLE "FinancialAccount" ADD COLUMN     "isBusiness" BOOLEAN NOT NULL DEFAULT false;

-- AlterTable
ALTER TABLE "FinancialAccountAccess" ADD COLUMN     "memberId" UUID,
ADD COLUMN     "organizationId" UUID,
ALTER COLUMN "userId" DROP NOT NULL;

-- AlterTable
ALTER TABLE "Organization" ADD COLUMN     "entityType" TEXT,
ADD COLUMN     "externalServiceId" TEXT NOT NULL DEFAULT '',
ADD COLUMN     "externalServiceType" "ExternalServiceType",
ADD COLUMN     "formedAt" TIMESTAMP(3),
ADD COLUMN     "industry" TEXT;

-- CreateTable
CREATE TABLE "Owner" (
    "modelName" "OwnerModelName" NOT NULL DEFAULT 'Owner',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "memberId" UUID NOT NULL,
    "title" TEXT NOT NULL DEFAULT '',
    "ownershipPercentage" INTEGER,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "isHidden" BOOLEAN NOT NULL DEFAULT false,
    "externalServiceType" "ExternalServiceType",
    "externalServiceId" TEXT NOT NULL DEFAULT '',
    "organizationId" UUID NOT NULL,

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

-- AddForeignKey
ALTER TABLE "Address" ADD CONSTRAINT "Address_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Address" ADD CONSTRAINT "Address_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
-- This delete cascades, because if a member is hard-deleted then the associated user should
-- certainly never have access to the organization's financial accounts.  There's no reason to
-- restrict deletes here.
ALTER TABLE "FinancialAccountAccess" ADD CONSTRAINT "FinancialAccountAccess_memberId_fkey" FOREIGN KEY ("memberId") REFERENCES "Member"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
-- Restrict deletes, because what does it mean to delete an org but not its financial accounts?
ALTER TABLE "FinancialAccountAccess" ADD CONSTRAINT "FinancialAccountAccess_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
-- Restrict deletes, because what does it mean to delete a user but not their financial accounts?
ALTER TABLE "FinancialAccountAccess" ADD CONSTRAINT "FinancialAccountAccess_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Owner" ADD CONSTRAINT "Owner_memberId_fkey" FOREIGN KEY ("memberId") REFERENCES "Member"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Owner" ADD CONSTRAINT "Owner_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
