-- CreateEnum
CREATE TYPE "SystemPermission" AS ENUM ('COMPETITOR_ANALYSIS', 'USER_MANAGEMENT', 'STORE_MANAGEMENT');

-- CreateEnum
CREATE TYPE "TicketStatus" AS ENUM ('OPEN', 'IN_PROGRESS', 'RESOLVED', 'CLOSED');

-- CreateTable
CREATE TABLE "companies" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "address" TEXT,
    "taxOffice" TEXT,
    "taxNumber" TEXT,
    "contactPerson" TEXT,
    "phone" TEXT,
    "email" TEXT,
    "isApproved" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "users" (
    "id" SERIAL NOT NULL,
    "email" TEXT NOT NULL,
    "password" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "surname" TEXT NOT NULL,
    "phoneNumber" TEXT,
    "companyId" INTEGER NOT NULL,
    "isAdmin" BOOLEAN NOT NULL DEFAULT false,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "global_admins" (
    "id" SERIAL NOT NULL,
    "email" TEXT NOT NULL,
    "password" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "surname" TEXT NOT NULL,
    "phoneNumber" TEXT,
    "isSuperAdmin" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "packages" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "logo" TEXT,
    "maxProducts" INTEGER NOT NULL DEFAULT 100,
    "frequencyMinutes" INTEGER NOT NULL DEFAULT 60,
    "maxConcurrentWorkers" INTEGER NOT NULL DEFAULT 1,
    "price" DOUBLE PRECISION NOT NULL DEFAULT 0.0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "maxStores" INTEGER NOT NULL DEFAULT 1,

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

-- CreateTable
CREATE TABLE "package_descriptions" (
    "id" SERIAL NOT NULL,
    "packageId" INTEGER NOT NULL,
    "description" TEXT NOT NULL,
    "order" INTEGER NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "marketplaces" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "bgColor" TEXT,
    "icon" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "company_marketplaces" (
    "id" SERIAL NOT NULL,
    "companyId" INTEGER NOT NULL,
    "marketplaceId" INTEGER NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "name" TEXT NOT NULL,
    "credentials" JSONB,

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

-- CreateTable
CREATE TABLE "company_marketplace_tokens" (
    "id" SERIAL NOT NULL,
    "companyMarketplaceId" INTEGER NOT NULL,
    "accessToken" TEXT NOT NULL,
    "refreshToken" TEXT,
    "expiresAt" TIMESTAMP(3),
    "tokenType" TEXT,
    "scope" TEXT,
    "data" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "user_marketplace_permissions" (
    "id" SERIAL NOT NULL,
    "userId" INTEGER NOT NULL,
    "companyMarketplaceId" INTEGER NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "UserSystemPermission" (
    "id" SERIAL NOT NULL,
    "userId" INTEGER NOT NULL,
    "permission" "SystemPermission" NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "company_packages" (
    "id" SERIAL NOT NULL,
    "companyId" INTEGER NOT NULL,
    "packageId" INTEGER NOT NULL,
    "status" BOOLEAN NOT NULL DEFAULT true,
    "packageEndDate" TIMESTAMP(3) NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "price" DOUBLE PRECISION,
    "maxProducts" INTEGER,
    "frequencyMinutes" INTEGER,
    "maxConcurrentWorkers" INTEGER,
    "maxStores" INTEGER,

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

-- CreateTable
CREATE TABLE "products" (
    "id" SERIAL NOT NULL,
    "companyId" INTEGER NOT NULL,
    "title" TEXT NOT NULL,
    "cost" DOUBLE PRECISION NOT NULL,
    "desi" INTEGER,
    "isbn" TEXT NOT NULL,
    "stockCode" TEXT NOT NULL,
    "category" TEXT,
    "brand" TEXT,
    "quantity" INTEGER,
    "psf" DOUBLE PRECISION,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "product_marketplaces" (
    "id" SERIAL NOT NULL,
    "productId" INTEGER NOT NULL,
    "companyMarketplaceId" INTEGER NOT NULL,
    "productUrl" TEXT,
    "marketplaceCode" TEXT,
    "commissionRate" DOUBLE PRECISION DEFAULT 10.0,
    "isActive" BOOLEAN NOT NULL DEFAULT false,
    "isManual" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "product_analyses" (
    "id" SERIAL NOT NULL,
    "salePrice" DOUBLE PRECISION,
    "discountedPrice" DOUBLE PRECISION,
    "commissionAmount" DOUBLE PRECISION,
    "shipping" DOUBLE PRECISION,
    "netProfit" DOUBLE PRECISION,
    "netProfitRate" DOUBLE PRECISION,
    "buyboxRank" INTEGER,
    "buyboxPrice" DOUBLE PRECISION,
    "buyboxProfit" DOUBLE PRECISION,
    "buyboxProfitRate" DOUBLE PRECISION,
    "isActiveOnMarketplace" BOOLEAN NOT NULL DEFAULT true,
    "competitors" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "productMarketplaceId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "marketplace_shipping_default_fees" (
    "id" SERIAL NOT NULL,
    "marketplaceId" INTEGER NOT NULL,
    "desi" INTEGER NOT NULL,
    "shippingFee" DOUBLE PRECISION NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "company_marketplace_shipping_fees" (
    "id" SERIAL NOT NULL,
    "companyMarketplaceId" INTEGER NOT NULL,
    "desi" INTEGER NOT NULL,
    "shippingFee" DOUBLE PRECISION NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "announcements" (
    "id" SERIAL NOT NULL,
    "title" TEXT NOT NULL,
    "content" TEXT NOT NULL,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "tickets" (
    "id" SERIAL NOT NULL,
    "userId" INTEGER NOT NULL,
    "title" TEXT NOT NULL,
    "status" "TicketStatus" NOT NULL DEFAULT 'OPEN',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ticket_messages" (
    "id" SERIAL NOT NULL,
    "ticketId" INTEGER NOT NULL,
    "userId" INTEGER,
    "globalAdminId" INTEGER,
    "message" TEXT NOT NULL,
    "isFromAdmin" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "market_data" (
    "id" SERIAL NOT NULL,
    "symbol" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "value" DOUBLE PRECISION NOT NULL,
    "changePercent" DOUBLE PRECISION NOT NULL,
    "unit" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "companies_name_key" ON "companies"("name");

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

-- CreateIndex
CREATE UNIQUE INDEX "global_admins_email_key" ON "global_admins"("email");

-- CreateIndex
CREATE UNIQUE INDEX "packages_name_key" ON "packages"("name");

-- CreateIndex
CREATE UNIQUE INDEX "marketplaces_name_key" ON "marketplaces"("name");

-- CreateIndex
CREATE UNIQUE INDEX "company_marketplaces_companyId_marketplaceId_name_key" ON "company_marketplaces"("companyId", "marketplaceId", "name");

-- CreateIndex
CREATE UNIQUE INDEX "company_marketplace_tokens_companyMarketplaceId_key" ON "company_marketplace_tokens"("companyMarketplaceId");

-- CreateIndex
CREATE UNIQUE INDEX "user_marketplace_permissions_userId_companyMarketplaceId_key" ON "user_marketplace_permissions"("userId", "companyMarketplaceId");

-- CreateIndex
CREATE UNIQUE INDEX "UserSystemPermission_userId_permission_key" ON "UserSystemPermission"("userId", "permission");

-- CreateIndex
CREATE UNIQUE INDEX "products_companyId_stockCode_key" ON "products"("companyId", "stockCode");

-- CreateIndex
CREATE INDEX "product_marketplaces_productId_companyMarketplaceId_idx" ON "product_marketplaces"("productId", "companyMarketplaceId");

-- CreateIndex
CREATE UNIQUE INDEX "marketplace_shipping_default_fees_marketplaceId_desi_key" ON "marketplace_shipping_default_fees"("marketplaceId", "desi");

-- CreateIndex
CREATE UNIQUE INDEX "market_data_symbol_key" ON "market_data"("symbol");

-- AddForeignKey
ALTER TABLE "users" ADD CONSTRAINT "users_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "companies"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "package_descriptions" ADD CONSTRAINT "package_descriptions_packageId_fkey" FOREIGN KEY ("packageId") REFERENCES "packages"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "company_marketplaces" ADD CONSTRAINT "company_marketplaces_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "companies"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "company_marketplaces" ADD CONSTRAINT "company_marketplaces_marketplaceId_fkey" FOREIGN KEY ("marketplaceId") REFERENCES "marketplaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_marketplace_permissions" ADD CONSTRAINT "user_marketplace_permissions_companyMarketplaceId_fkey" FOREIGN KEY ("companyMarketplaceId") REFERENCES "company_marketplaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_marketplace_permissions" ADD CONSTRAINT "user_marketplace_permissions_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UserSystemPermission" ADD CONSTRAINT "UserSystemPermission_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "company_packages" ADD CONSTRAINT "company_packages_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "companies"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "company_packages" ADD CONSTRAINT "company_packages_packageId_fkey" FOREIGN KEY ("packageId") REFERENCES "packages"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "products" ADD CONSTRAINT "products_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "companies"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "product_marketplaces" ADD CONSTRAINT "product_marketplaces_companyMarketplaceId_fkey" FOREIGN KEY ("companyMarketplaceId") REFERENCES "company_marketplaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "product_marketplaces" ADD CONSTRAINT "product_marketplaces_productId_fkey" FOREIGN KEY ("productId") REFERENCES "products"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "product_analyses" ADD CONSTRAINT "product_analyses_productMarketplaceId_fkey" FOREIGN KEY ("productMarketplaceId") REFERENCES "product_marketplaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "marketplace_shipping_default_fees" ADD CONSTRAINT "marketplace_shipping_default_fees_marketplaceId_fkey" FOREIGN KEY ("marketplaceId") REFERENCES "marketplaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "company_marketplace_shipping_fees" ADD CONSTRAINT "company_marketplace_shipping_fees_companyMarketplaceId_fkey" FOREIGN KEY ("companyMarketplaceId") REFERENCES "company_marketplaces"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tickets" ADD CONSTRAINT "tickets_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ticket_messages" ADD CONSTRAINT "ticket_messages_ticketId_fkey" FOREIGN KEY ("ticketId") REFERENCES "tickets"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ticket_messages" ADD CONSTRAINT "ticket_messages_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ticket_messages" ADD CONSTRAINT "ticket_messages_globalAdminId_fkey" FOREIGN KEY ("globalAdminId") REFERENCES "global_admins"("id") ON DELETE SET NULL ON UPDATE CASCADE;
