-- CreateEnum
CREATE TYPE "Role" AS ENUM ('SUPER_ADMIN', 'PROJECT_MANAGER', 'ADMIN_STAFF', 'FINANCE_OFFICER', 'BOARD_MEMBER', 'VENDOR');

-- CreateEnum
CREATE TYPE "TenderStatus" AS ENUM ('DRAFT', 'PUBLISHED', 'SUBMISSIONS_RECEIVED', 'UNDER_ANALYSIS', 'BOARD_REVIEW', 'AWARDED', 'REJECTED', 'RE_TENDERED');

-- CreateEnum
CREATE TYPE "ItemStatus" AS ENUM ('ACTIVE', 'DISCONTINUED', 'UNDER_REVIEW');

-- CreateEnum
CREATE TYPE "VendorVerificationStatus" AS ENUM ('PENDING', 'VERIFIED', 'REJECTED');

-- CreateEnum
CREATE TYPE "VendorStatus" AS ENUM ('PENDING', 'APPROVED', 'REJECTED', 'DEACTIVATED');

-- CreateEnum
CREATE TYPE "BidStatus" AS ENUM ('DRAFT', 'PENDING_APPROVAL', 'SUBMITTED', 'WITHDRAWN', 'APPROVED', 'REJECTED');

-- CreateEnum
CREATE TYPE "BoardDecisionStatus" AS ENUM ('PENDING', 'APPROVED', 'REJECTED', 'RETURNED');

-- CreateEnum
CREATE TYPE "TenderType" AS ENUM ('PROJECT', 'CATEGORY');

-- CreateEnum
CREATE TYPE "TenderItemSource" AS ENUM ('MASTER_ITEM', 'PROJECT_SPECIFIC_ITEM');

-- CreateEnum
CREATE TYPE "BundleStatus" AS ENUM ('DRAFT', 'PUBLISHED', 'AWARDED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "AssignmentType" AS ENUM ('PROJECT', 'CATEGORY');

-- CreateTable
CREATE TABLE "FiscalYear" (
    "id" TEXT NOT NULL,
    "year" TEXT NOT NULL,
    "startDate" TIMESTAMP(3) NOT NULL,
    "endDate" TIMESTAMP(3) NOT NULL,
    "isActive" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Category" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "description" TEXT,
    "parentId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ProjectType" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "description" TEXT,
    "pdfNote" TEXT,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "SystemSetting" (
    "id" TEXT NOT NULL,
    "key" TEXT NOT NULL,
    "value" TEXT NOT NULL,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "User" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "password" TEXT NOT NULL,
    "role" "Role" NOT NULL DEFAULT 'VENDOR',
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "emailVerified" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Vendor" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "phone" TEXT,
    "vatPan" TEXT NOT NULL,
    "address" TEXT,
    "contactPerson" TEXT,
    "status" "VendorStatus" NOT NULL DEFAULT 'PENDING',
    "verificationStatus" "VendorVerificationStatus" NOT NULL DEFAULT 'PENDING',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "VendorDocument" (
    "id" TEXT NOT NULL,
    "vendorId" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "url" TEXT NOT NULL,
    "uploadedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "Item" (
    "id" TEXT NOT NULL,
    "code" TEXT,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "unit" TEXT NOT NULL,
    "defaultWeight" DECIMAL(12,4),
    "categoryId" TEXT NOT NULL,
    "status" "ItemStatus" NOT NULL DEFAULT 'ACTIVE',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ProjectSpecificItem" (
    "id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "unit" TEXT NOT NULL,
    "weight" DECIMAL(12,4),
    "quantity" DECIMAL(12,2) NOT NULL DEFAULT 0,
    "categoryId" TEXT,
    "status" "ItemStatus" NOT NULL DEFAULT 'ACTIVE',
    "projectId" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ItemRate" (
    "id" TEXT NOT NULL,
    "itemId" TEXT,
    "projectSpecificItemId" TEXT,
    "fiscalYearId" TEXT NOT NULL,
    "rate" DECIMAL(12,2) NOT NULL,
    "source" TEXT,
    "sourceRef" TEXT,
    "justification" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Project" (
    "id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "division" TEXT,
    "province" TEXT,
    "budget" DECIMAL(15,2) NOT NULL,
    "fiscalYearId" TEXT NOT NULL,
    "typeId" TEXT,
    "status" TEXT,
    "createdById" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ProjectItem" (
    "id" TEXT NOT NULL,
    "projectId" TEXT NOT NULL,
    "itemId" TEXT NOT NULL,
    "quantity" DECIMAL(12,2) NOT NULL,
    "estimatedRate" DECIMAL(12,2),
    "notes" TEXT,
    "weight" DECIMAL(12,4),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "TenderBundle" (
    "id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "projectId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "estimatedTotal" DECIMAL(15,2),
    "status" "TenderStatus" NOT NULL DEFAULT 'DRAFT',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "TenderBundleItem" (
    "id" TEXT NOT NULL,
    "bundleId" TEXT NOT NULL,
    "itemId" TEXT,
    "quantity" DECIMAL(12,2) NOT NULL,
    "estimatedRate" DECIMAL(12,2),
    "weight" DECIMAL(12,4),
    "specifications" TEXT,
    "projectSpecificItemId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "CategoryBundle" (
    "id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "pdfNote" TEXT,
    "fiscalYearId" TEXT NOT NULL,
    "categoryId" TEXT NOT NULL,
    "status" "BundleStatus" NOT NULL DEFAULT 'DRAFT',
    "estimatedTotal" DECIMAL(15,2) NOT NULL DEFAULT 0,
    "version" INTEGER NOT NULL DEFAULT 1,
    "createdById" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "CategoryBundleItem" (
    "id" TEXT NOT NULL,
    "bundleId" TEXT NOT NULL,
    "itemId" TEXT NOT NULL,
    "aggregatedQty" DECIMAL(12,2) NOT NULL,
    "estimatedRate" DECIMAL(12,2) NOT NULL,
    "estimatedTotal" DECIMAL(15,2) NOT NULL,
    "weight" DECIMAL(12,4),
    "specifications" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "CategoryBundleItemSource" (
    "id" TEXT NOT NULL,
    "bundleItemId" TEXT NOT NULL,
    "projectId" TEXT NOT NULL,
    "quantity" DECIMAL(12,2) NOT NULL,
    "rate" DECIMAL(12,2) NOT NULL,
    "total" DECIMAL(15,2) NOT NULL,
    "included" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "CategoryBundleProject" (
    "id" TEXT NOT NULL,
    "bundleId" TEXT NOT NULL,
    "projectId" TEXT NOT NULL,
    "included" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "CategoryBundleRevision" (
    "id" TEXT NOT NULL,
    "bundleId" TEXT NOT NULL,
    "version" INTEGER NOT NULL,
    "changes" JSONB NOT NULL,
    "createdById" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "notes" TEXT,

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

-- CreateTable
CREATE TABLE "ItemAssignment" (
    "id" TEXT NOT NULL,
    "itemId" TEXT NOT NULL,
    "projectId" TEXT NOT NULL,
    "assignedTo" "AssignmentType",
    "tenderBundleId" TEXT,
    "categoryBundleId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "TenderNotice" (
    "id" TEXT NOT NULL,
    "referenceNo" TEXT NOT NULL,
    "bundleId" TEXT,
    "fiscalYearId" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "eligibility" TEXT,
    "submissionDeadline" TIMESTAMP(3) NOT NULL,
    "status" "TenderStatus" NOT NULL DEFAULT 'DRAFT',
    "tenderType" "TenderType" NOT NULL DEFAULT 'PROJECT',
    "projectId" TEXT,
    "categoryBundleId" TEXT,
    "publishedAt" TIMESTAMP(3),
    "autoApproveBids" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "TenderVendor" (
    "id" TEXT NOT NULL,
    "tenderId" TEXT NOT NULL,
    "vendorId" TEXT NOT NULL,
    "invitedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "BidSubmission" (
    "id" TEXT NOT NULL,
    "tenderId" TEXT NOT NULL,
    "vendorId" TEXT NOT NULL,
    "referenceNo" TEXT NOT NULL,
    "status" "BidStatus" NOT NULL DEFAULT 'DRAFT',
    "totalAmount" DECIMAL(15,2),
    "submittedAt" TIMESTAMP(3),
    "approvedAt" TIMESTAMP(3),
    "approvedById" TEXT,
    "rejectedAt" TIMESTAMP(3),
    "rejectedById" TEXT,
    "rejectionReason" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "BidLineItem" (
    "id" TEXT NOT NULL,
    "submissionId" TEXT NOT NULL,
    "itemId" TEXT,
    "quotedRate" DECIMAL(12,2) NOT NULL,
    "quantity" DECIMAL(12,2) NOT NULL,
    "totalAmount" DECIMAL(15,2) NOT NULL,
    "projectSpecificItemId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "BidVersion" (
    "id" TEXT NOT NULL,
    "submissionId" TEXT NOT NULL,
    "version" INTEGER NOT NULL,
    "lineItems" JSONB NOT NULL,
    "totalAmount" DECIMAL(15,2),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "ComparativeAnalysis" (
    "id" TEXT NOT NULL,
    "tenderId" TEXT NOT NULL,
    "preparedById" TEXT NOT NULL,
    "analysisData" JSONB NOT NULL,
    "recommendation" TEXT,
    "conditions" TEXT,
    "scoreData" JSONB,
    "submittedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "BoardDecision" (
    "id" TEXT NOT NULL,
    "analysisId" TEXT NOT NULL,
    "memberId" TEXT NOT NULL,
    "decision" "BoardDecisionStatus" NOT NULL DEFAULT 'PENDING',
    "remarks" TEXT,
    "votedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "BoardConfig" (
    "id" TEXT NOT NULL,
    "fiscalYearId" TEXT NOT NULL,
    "requiredMembers" INTEGER NOT NULL DEFAULT 3,
    "quorumCount" INTEGER NOT NULL DEFAULT 2,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "AwardRecord" (
    "id" TEXT NOT NULL,
    "tenderId" TEXT NOT NULL,
    "vendorId" TEXT NOT NULL,
    "referenceNo" TEXT NOT NULL,
    "totalAmount" DECIMAL(15,2) NOT NULL,
    "awardLetterPath" TEXT,
    "status" TEXT,
    "fiscalYearId" TEXT NOT NULL,
    "awardedAt" TIMESTAMP(3) NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "RejectionRecord" (
    "id" TEXT NOT NULL,
    "awardId" TEXT NOT NULL,
    "vendorId" TEXT NOT NULL,
    "reason" TEXT,
    "letterPath" TEXT,
    "status" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "AuditLog" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "action" TEXT NOT NULL,
    "entityType" TEXT NOT NULL,
    "entityId" TEXT,
    "entityName" TEXT,
    "changes" JSONB,
    "ipAddress" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "Notification" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "message" TEXT NOT NULL,
    "link" TEXT,
    "readAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "Account" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "provider" TEXT NOT NULL,
    "providerAccountId" TEXT NOT NULL,
    "refresh_token" TEXT,
    "access_token" TEXT,
    "expires_at" INTEGER,
    "token_type" TEXT,
    "scope" TEXT,
    "id_token" TEXT,
    "session_state" TEXT,
    "refresh_token_expires_in" INTEGER,

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

-- CreateTable
CREATE TABLE "Session" (
    "id" TEXT NOT NULL,
    "sessionToken" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "expires" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "VerificationToken" (
    "identifier" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "expires" TIMESTAMP(3) NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "FiscalYear_year_key" ON "FiscalYear"("year");

-- CreateIndex
CREATE UNIQUE INDEX "Category_code_key" ON "Category"("code");

-- CreateIndex
CREATE UNIQUE INDEX "ProjectType_code_key" ON "ProjectType"("code");

-- CreateIndex
CREATE UNIQUE INDEX "SystemSetting_key_key" ON "SystemSetting"("key");

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- CreateIndex
CREATE UNIQUE INDEX "Vendor_email_key" ON "Vendor"("email");

-- CreateIndex
CREATE UNIQUE INDEX "Vendor_vatPan_key" ON "Vendor"("vatPan");

-- CreateIndex
CREATE UNIQUE INDEX "Item_code_key" ON "Item"("code");

-- CreateIndex
CREATE UNIQUE INDEX "ProjectSpecificItem_projectId_code_key" ON "ProjectSpecificItem"("projectId", "code");

-- CreateIndex
CREATE UNIQUE INDEX "ItemRate_itemId_fiscalYearId_key" ON "ItemRate"("itemId", "fiscalYearId");

-- CreateIndex
CREATE UNIQUE INDEX "ItemRate_projectSpecificItemId_fiscalYearId_key" ON "ItemRate"("projectSpecificItemId", "fiscalYearId");

-- CreateIndex
CREATE UNIQUE INDEX "Project_code_key" ON "Project"("code");

-- CreateIndex
CREATE UNIQUE INDEX "ProjectItem_projectId_itemId_key" ON "ProjectItem"("projectId", "itemId");

-- CreateIndex
CREATE UNIQUE INDEX "TenderBundle_code_key" ON "TenderBundle"("code");

-- CreateIndex
CREATE UNIQUE INDEX "CategoryBundle_code_key" ON "CategoryBundle"("code");

-- CreateIndex
CREATE INDEX "CategoryBundle_fiscalYearId_categoryId_idx" ON "CategoryBundle"("fiscalYearId", "categoryId");

-- CreateIndex
CREATE INDEX "CategoryBundleItem_bundleId_idx" ON "CategoryBundleItem"("bundleId");

-- CreateIndex
CREATE UNIQUE INDEX "CategoryBundleItem_bundleId_itemId_key" ON "CategoryBundleItem"("bundleId", "itemId");

-- CreateIndex
CREATE INDEX "CategoryBundleItemSource_bundleItemId_idx" ON "CategoryBundleItemSource"("bundleItemId");

-- CreateIndex
CREATE UNIQUE INDEX "CategoryBundleItemSource_bundleItemId_projectId_key" ON "CategoryBundleItemSource"("bundleItemId", "projectId");

-- CreateIndex
CREATE UNIQUE INDEX "CategoryBundleProject_bundleId_projectId_key" ON "CategoryBundleProject"("bundleId", "projectId");

-- CreateIndex
CREATE INDEX "CategoryBundleRevision_bundleId_idx" ON "CategoryBundleRevision"("bundleId");

-- CreateIndex
CREATE UNIQUE INDEX "CategoryBundleRevision_bundleId_version_key" ON "CategoryBundleRevision"("bundleId", "version");

-- CreateIndex
CREATE INDEX "ItemAssignment_itemId_idx" ON "ItemAssignment"("itemId");

-- CreateIndex
CREATE INDEX "ItemAssignment_projectId_idx" ON "ItemAssignment"("projectId");

-- CreateIndex
CREATE UNIQUE INDEX "ItemAssignment_itemId_projectId_key" ON "ItemAssignment"("itemId", "projectId");

-- CreateIndex
CREATE UNIQUE INDEX "TenderNotice_referenceNo_key" ON "TenderNotice"("referenceNo");

-- CreateIndex
CREATE UNIQUE INDEX "TenderNotice_categoryBundleId_key" ON "TenderNotice"("categoryBundleId");

-- CreateIndex
CREATE UNIQUE INDEX "TenderVendor_tenderId_vendorId_key" ON "TenderVendor"("tenderId", "vendorId");

-- CreateIndex
CREATE UNIQUE INDEX "BidSubmission_referenceNo_key" ON "BidSubmission"("referenceNo");

-- CreateIndex
CREATE UNIQUE INDEX "BidSubmission_tenderId_vendorId_key" ON "BidSubmission"("tenderId", "vendorId");

-- CreateIndex
CREATE INDEX "BidVersion_submissionId_idx" ON "BidVersion"("submissionId");

-- CreateIndex
CREATE UNIQUE INDEX "ComparativeAnalysis_tenderId_key" ON "ComparativeAnalysis"("tenderId");

-- CreateIndex
CREATE UNIQUE INDEX "BoardDecision_analysisId_memberId_key" ON "BoardDecision"("analysisId", "memberId");

-- CreateIndex
CREATE UNIQUE INDEX "BoardConfig_fiscalYearId_key" ON "BoardConfig"("fiscalYearId");

-- CreateIndex
CREATE UNIQUE INDEX "AwardRecord_tenderId_key" ON "AwardRecord"("tenderId");

-- CreateIndex
CREATE UNIQUE INDEX "AwardRecord_referenceNo_key" ON "AwardRecord"("referenceNo");

-- CreateIndex
CREATE UNIQUE INDEX "Account_provider_providerAccountId_key" ON "Account"("provider", "providerAccountId");

-- CreateIndex
CREATE UNIQUE INDEX "Session_sessionToken_key" ON "Session"("sessionToken");

-- CreateIndex
CREATE UNIQUE INDEX "VerificationToken_token_key" ON "VerificationToken"("token");

-- CreateIndex
CREATE UNIQUE INDEX "VerificationToken_identifier_token_key" ON "VerificationToken"("identifier", "token");

-- AddForeignKey
ALTER TABLE "Category" ADD CONSTRAINT "Category_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "Category"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "VendorDocument" ADD CONSTRAINT "VendorDocument_vendorId_fkey" FOREIGN KEY ("vendorId") REFERENCES "Vendor"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Item" ADD CONSTRAINT "Item_categoryId_fkey" FOREIGN KEY ("categoryId") REFERENCES "Category"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ProjectSpecificItem" ADD CONSTRAINT "ProjectSpecificItem_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ProjectSpecificItem" ADD CONSTRAINT "ProjectSpecificItem_categoryId_fkey" FOREIGN KEY ("categoryId") REFERENCES "Category"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ItemRate" ADD CONSTRAINT "ItemRate_itemId_fkey" FOREIGN KEY ("itemId") REFERENCES "Item"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ItemRate" ADD CONSTRAINT "ItemRate_projectSpecificItemId_fkey" FOREIGN KEY ("projectSpecificItemId") REFERENCES "ProjectSpecificItem"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ItemRate" ADD CONSTRAINT "ItemRate_fiscalYearId_fkey" FOREIGN KEY ("fiscalYearId") REFERENCES "FiscalYear"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Project" ADD CONSTRAINT "Project_fiscalYearId_fkey" FOREIGN KEY ("fiscalYearId") REFERENCES "FiscalYear"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Project" ADD CONSTRAINT "Project_typeId_fkey" FOREIGN KEY ("typeId") REFERENCES "ProjectType"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Project" ADD CONSTRAINT "Project_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ProjectItem" ADD CONSTRAINT "ProjectItem_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ProjectItem" ADD CONSTRAINT "ProjectItem_itemId_fkey" FOREIGN KEY ("itemId") REFERENCES "Item"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TenderBundle" ADD CONSTRAINT "TenderBundle_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TenderBundleItem" ADD CONSTRAINT "TenderBundleItem_bundleId_fkey" FOREIGN KEY ("bundleId") REFERENCES "TenderBundle"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TenderBundleItem" ADD CONSTRAINT "TenderBundleItem_itemId_fkey" FOREIGN KEY ("itemId") REFERENCES "Item"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TenderBundleItem" ADD CONSTRAINT "TenderBundleItem_projectSpecificItemId_fkey" FOREIGN KEY ("projectSpecificItemId") REFERENCES "ProjectSpecificItem"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CategoryBundle" ADD CONSTRAINT "CategoryBundle_fiscalYearId_fkey" FOREIGN KEY ("fiscalYearId") REFERENCES "FiscalYear"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CategoryBundle" ADD CONSTRAINT "CategoryBundle_categoryId_fkey" FOREIGN KEY ("categoryId") REFERENCES "Category"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CategoryBundle" ADD CONSTRAINT "CategoryBundle_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CategoryBundleItem" ADD CONSTRAINT "CategoryBundleItem_bundleId_fkey" FOREIGN KEY ("bundleId") REFERENCES "CategoryBundle"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CategoryBundleItem" ADD CONSTRAINT "CategoryBundleItem_itemId_fkey" FOREIGN KEY ("itemId") REFERENCES "Item"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CategoryBundleItemSource" ADD CONSTRAINT "CategoryBundleItemSource_bundleItemId_fkey" FOREIGN KEY ("bundleItemId") REFERENCES "CategoryBundleItem"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CategoryBundleItemSource" ADD CONSTRAINT "CategoryBundleItemSource_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CategoryBundleProject" ADD CONSTRAINT "CategoryBundleProject_bundleId_fkey" FOREIGN KEY ("bundleId") REFERENCES "CategoryBundle"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CategoryBundleProject" ADD CONSTRAINT "CategoryBundleProject_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CategoryBundleRevision" ADD CONSTRAINT "CategoryBundleRevision_bundleId_fkey" FOREIGN KEY ("bundleId") REFERENCES "CategoryBundle"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CategoryBundleRevision" ADD CONSTRAINT "CategoryBundleRevision_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ItemAssignment" ADD CONSTRAINT "ItemAssignment_itemId_fkey" FOREIGN KEY ("itemId") REFERENCES "Item"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ItemAssignment" ADD CONSTRAINT "ItemAssignment_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TenderNotice" ADD CONSTRAINT "TenderNotice_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TenderNotice" ADD CONSTRAINT "TenderNotice_bundleId_fkey" FOREIGN KEY ("bundleId") REFERENCES "TenderBundle"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TenderNotice" ADD CONSTRAINT "TenderNotice_categoryBundleId_fkey" FOREIGN KEY ("categoryBundleId") REFERENCES "CategoryBundle"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TenderNotice" ADD CONSTRAINT "TenderNotice_fiscalYearId_fkey" FOREIGN KEY ("fiscalYearId") REFERENCES "FiscalYear"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TenderVendor" ADD CONSTRAINT "TenderVendor_tenderId_fkey" FOREIGN KEY ("tenderId") REFERENCES "TenderNotice"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TenderVendor" ADD CONSTRAINT "TenderVendor_vendorId_fkey" FOREIGN KEY ("vendorId") REFERENCES "Vendor"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BidSubmission" ADD CONSTRAINT "BidSubmission_tenderId_fkey" FOREIGN KEY ("tenderId") REFERENCES "TenderNotice"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BidSubmission" ADD CONSTRAINT "BidSubmission_approvedById_fkey" FOREIGN KEY ("approvedById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BidSubmission" ADD CONSTRAINT "BidSubmission_rejectedById_fkey" FOREIGN KEY ("rejectedById") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BidSubmission" ADD CONSTRAINT "BidSubmission_vendorId_fkey" FOREIGN KEY ("vendorId") REFERENCES "Vendor"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BidLineItem" ADD CONSTRAINT "BidLineItem_submissionId_fkey" FOREIGN KEY ("submissionId") REFERENCES "BidSubmission"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BidLineItem" ADD CONSTRAINT "BidLineItem_itemId_fkey" FOREIGN KEY ("itemId") REFERENCES "Item"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BidLineItem" ADD CONSTRAINT "BidLineItem_projectSpecificItemId_fkey" FOREIGN KEY ("projectSpecificItemId") REFERENCES "ProjectSpecificItem"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BidVersion" ADD CONSTRAINT "BidVersion_submissionId_fkey" FOREIGN KEY ("submissionId") REFERENCES "BidSubmission"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ComparativeAnalysis" ADD CONSTRAINT "ComparativeAnalysis_tenderId_fkey" FOREIGN KEY ("tenderId") REFERENCES "TenderNotice"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ComparativeAnalysis" ADD CONSTRAINT "ComparativeAnalysis_preparedById_fkey" FOREIGN KEY ("preparedById") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BoardDecision" ADD CONSTRAINT "BoardDecision_analysisId_fkey" FOREIGN KEY ("analysisId") REFERENCES "ComparativeAnalysis"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BoardDecision" ADD CONSTRAINT "BoardDecision_memberId_fkey" FOREIGN KEY ("memberId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "BoardConfig" ADD CONSTRAINT "BoardConfig_fiscalYearId_fkey" FOREIGN KEY ("fiscalYearId") REFERENCES "FiscalYear"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AwardRecord" ADD CONSTRAINT "AwardRecord_tenderId_fkey" FOREIGN KEY ("tenderId") REFERENCES "TenderNotice"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AwardRecord" ADD CONSTRAINT "AwardRecord_vendorId_fkey" FOREIGN KEY ("vendorId") REFERENCES "Vendor"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AwardRecord" ADD CONSTRAINT "AwardRecord_fiscalYearId_fkey" FOREIGN KEY ("fiscalYearId") REFERENCES "FiscalYear"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RejectionRecord" ADD CONSTRAINT "RejectionRecord_awardId_fkey" FOREIGN KEY ("awardId") REFERENCES "AwardRecord"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RejectionRecord" ADD CONSTRAINT "RejectionRecord_vendorId_fkey" FOREIGN KEY ("vendorId") REFERENCES "Vendor"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

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

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

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