/*
  Warnings:

  - You are about to drop the column `avatar` on the `users` table. All the data in the column will be lost.
  - You are about to drop the column `created_time` on the `users` table. All the data in the column will be lost.
  - You are about to drop the column `deactivated_time` on the `users` table. All the data in the column will be lost.
  - You are about to drop the column `deleted_time` on the `users` table. All the data in the column will be lost.
  - You are about to drop the column `is_admin` on the `users` table. All the data in the column will be lost.
  - You are about to drop the column `is_system` on the `users` table. All the data in the column will be lost.
  - You are about to drop the column `last_modified_time` on the `users` table. All the data in the column will be lost.
  - You are about to drop the column `last_sign_time` on the `users` table. All the data in the column will be lost.
  - You are about to drop the column `password` on the `users` table. All the data in the column will be lost.
  - You are about to drop the column `phone` on the `users` table. All the data in the column will be lost.
  - You are about to drop the column `salt` on the `users` table. All the data in the column will be lost.
  - You are about to drop the `attachments` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `notification` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `oidc_clients` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `resource` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `setting` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `trash` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `user_last_visit` table. If the table is not empty, all the data it contains will be lost.
  - A unique constraint covering the columns `[username]` on the table `users` will be added. If there are existing duplicate values, this will fail.
  - Added the required column `updatedAt` to the `users` table without a default value. This is not possible if the table is not empty.
  - Added the required column `username` to the `users` table without a default value. This is not possible if the table is not empty.

*/
-- CreateEnum
CREATE TYPE "Status" AS ENUM ('ACTIVE', 'INACTIVE');

-- CreateEnum
CREATE TYPE "SourceType" AS ENUM ('DATABASE', 'API', 'FILE', 'ERP', 'MES', 'IOT', 'STREAM');

-- CreateEnum
CREATE TYPE "PipelineType" AS ENUM ('SYNC', 'TRANSFORM', 'STREAM', 'HYBRID');

-- CreateEnum
CREATE TYPE "ExecutionStatus" AS ENUM ('PENDING', 'RUNNING', 'SUCCESS', 'FAILED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "DataFormat" AS ENUM ('PARQUET', 'JSON', 'CSV', 'AVRO', 'DELTA');

-- CreateEnum
CREATE TYPE "AssetType" AS ENUM ('BATCH', 'STREAM', 'TABLE', 'VIEW', 'MODEL');

-- CreateEnum
CREATE TYPE "AssetStatus" AS ENUM ('ACTIVE', 'ARCHIVED', 'DEPRECATED');

-- CreateEnum
CREATE TYPE "QueryEngine" AS ENUM ('DUCKDB', 'ATHENA', 'SPARK', 'TRINO');

-- CreateEnum
CREATE TYPE "QueryStatus" AS ENUM ('PENDING', 'RUNNING', 'SUCCESS', 'FAILED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "EntityType" AS ENUM ('PERSON', 'EQUIPMENT', 'FACILITY', 'MATERIAL', 'ORGANIZATION', 'PROJECT', 'CUSTOM');

-- CreateEnum
CREATE TYPE "LineageStatus" AS ENUM ('ACTIVE', 'ARCHIVED');

-- DropIndex
DROP INDEX "users_phone_key";

-- AlterTable
ALTER TABLE "users" DROP COLUMN "avatar",
DROP COLUMN "created_time",
DROP COLUMN "deactivated_time",
DROP COLUMN "deleted_time",
DROP COLUMN "is_admin",
DROP COLUMN "is_system",
DROP COLUMN "last_modified_time",
DROP COLUMN "last_sign_time",
DROP COLUMN "password",
DROP COLUMN "phone",
DROP COLUMN "salt",
ADD COLUMN     "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN     "roles" JSONB,
ADD COLUMN     "status" "Status" NOT NULL DEFAULT 'ACTIVE',
ADD COLUMN     "updatedAt" TIMESTAMP(3) NOT NULL,
ADD COLUMN     "username" TEXT NOT NULL;

-- DropTable
DROP TABLE "attachments";

-- DropTable
DROP TABLE "notification";

-- DropTable
DROP TABLE "oidc_clients";

-- DropTable
DROP TABLE "resource";

-- DropTable
DROP TABLE "setting";

-- DropTable
DROP TABLE "trash";

-- DropTable
DROP TABLE "user_last_visit";

-- CreateTable
CREATE TABLE "data_sources" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "type" "SourceType" NOT NULL,
    "config" JSONB NOT NULL,
    "description" TEXT,
    "status" "Status" NOT NULL DEFAULT 'ACTIVE',
    "schemaVersion" TEXT DEFAULT '1.0',
    "lastSynced" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "pipelines" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "type" "PipelineType" NOT NULL,
    "schedule" TEXT,
    "config" JSONB NOT NULL,
    "description" TEXT,
    "status" "Status" NOT NULL DEFAULT 'ACTIVE',
    "dataSourceId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "pipeline_executions" (
    "id" TEXT NOT NULL,
    "executionId" TEXT NOT NULL,
    "status" "ExecutionStatus" NOT NULL DEFAULT 'PENDING',
    "inputRecords" INTEGER NOT NULL DEFAULT 0,
    "outputRecords" INTEGER NOT NULL DEFAULT 0,
    "errorRecords" INTEGER NOT NULL DEFAULT 0,
    "startedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completedAt" TIMESTAMP(3),
    "duration" INTEGER,
    "metadata" JSONB,
    "errorMsg" TEXT,
    "pipelineId" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "data_assets" (
    "id" TEXT NOT NULL,
    "assetId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "type" "AssetType" NOT NULL DEFAULT 'BATCH',
    "format" "DataFormat" NOT NULL DEFAULT 'PARQUET',
    "storageConfig" JSONB NOT NULL,
    "schema" JSONB,
    "partitions" JSONB,
    "size" BIGINT,
    "recordCount" BIGINT,
    "qualityScore" DOUBLE PRECISION,
    "executionId" TEXT,
    "status" "AssetStatus" NOT NULL DEFAULT 'ACTIVE',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "queries" (
    "id" TEXT NOT NULL,
    "queryId" TEXT NOT NULL,
    "sql" TEXT NOT NULL,
    "engine" "QueryEngine" NOT NULL DEFAULT 'DUCKDB',
    "status" "QueryStatus" NOT NULL DEFAULT 'PENDING',
    "resultCount" BIGINT,
    "resultPath" TEXT,
    "duration" INTEGER,
    "errorMsg" TEXT,
    "tags" JSONB,
    "assetId" TEXT NOT NULL,
    "userId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completedAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "entities" (
    "id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "type" "EntityType" NOT NULL,
    "attributes" JSONB,
    "status" "Status" NOT NULL DEFAULT 'ACTIVE',
    "parentId" TEXT,
    "path" TEXT,
    "level" INTEGER,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "entity_relations" (
    "id" TEXT NOT NULL,
    "sourceId" TEXT NOT NULL,
    "targetId" TEXT NOT NULL,
    "relationship" TEXT NOT NULL,
    "attributes" JSONB,
    "startDate" TIMESTAMP(3),
    "endDate" TIMESTAMP(3),
    "metadata" JSONB,
    "status" "Status" NOT NULL DEFAULT 'ACTIVE',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "lineage_records" (
    "id" TEXT NOT NULL,
    "sourceType" TEXT NOT NULL,
    "sourceId" TEXT NOT NULL,
    "targetType" TEXT NOT NULL,
    "targetId" TEXT NOT NULL,
    "relationship" TEXT NOT NULL,
    "metadata" JSONB,
    "executionId" TEXT,
    "entityId" TEXT,
    "status" "LineageStatus" NOT NULL DEFAULT 'ACTIVE',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "quality_rules" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "rule" JSONB NOT NULL,
    "threshold" DOUBLE PRECISION,
    "entityType" TEXT,
    "entityId" TEXT,
    "status" "Status" NOT NULL DEFAULT 'ACTIVE',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "data_catalogs" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "tags" JSONB,
    "owner" TEXT,
    "category" TEXT,
    "sensitivity" TEXT,
    "assetType" TEXT NOT NULL,
    "assetId" TEXT NOT NULL,
    "status" "Status" NOT NULL DEFAULT 'ACTIVE',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "data_sources_code_key" ON "data_sources"("code");

-- CreateIndex
CREATE UNIQUE INDEX "pipeline_executions_executionId_key" ON "pipeline_executions"("executionId");

-- CreateIndex
CREATE UNIQUE INDEX "data_assets_assetId_key" ON "data_assets"("assetId");

-- CreateIndex
CREATE INDEX "data_assets_type_status_idx" ON "data_assets"("type", "status");

-- CreateIndex
CREATE INDEX "data_assets_createdAt_idx" ON "data_assets"("createdAt");

-- CreateIndex
CREATE UNIQUE INDEX "queries_queryId_key" ON "queries"("queryId");

-- CreateIndex
CREATE INDEX "queries_status_createdAt_idx" ON "queries"("status", "createdAt");

-- CreateIndex
CREATE UNIQUE INDEX "entities_code_key" ON "entities"("code");

-- CreateIndex
CREATE INDEX "entities_type_status_idx" ON "entities"("type", "status");

-- CreateIndex
CREATE INDEX "entities_code_idx" ON "entities"("code");

-- CreateIndex
CREATE INDEX "entities_type_code_idx" ON "entities"("type", "code");

-- CreateIndex
CREATE INDEX "entities_parentId_idx" ON "entities"("parentId");

-- CreateIndex
CREATE INDEX "entities_path_idx" ON "entities"("path");

-- CreateIndex
CREATE INDEX "entities_type_parentId_idx" ON "entities"("type", "parentId");

-- CreateIndex
CREATE INDEX "entities_level_idx" ON "entities"("level");

-- CreateIndex
CREATE INDEX "entity_relations_sourceId_idx" ON "entity_relations"("sourceId");

-- CreateIndex
CREATE INDEX "entity_relations_targetId_idx" ON "entity_relations"("targetId");

-- CreateIndex
CREATE INDEX "entity_relations_relationship_idx" ON "entity_relations"("relationship");

-- CreateIndex
CREATE INDEX "entity_relations_sourceId_relationship_idx" ON "entity_relations"("sourceId", "relationship");

-- CreateIndex
CREATE INDEX "entity_relations_targetId_relationship_idx" ON "entity_relations"("targetId", "relationship");

-- CreateIndex
CREATE INDEX "entity_relations_relationship_status_idx" ON "entity_relations"("relationship", "status");

-- CreateIndex
CREATE UNIQUE INDEX "entity_relations_sourceId_targetId_relationship_key" ON "entity_relations"("sourceId", "targetId", "relationship");

-- CreateIndex
CREATE INDEX "lineage_records_relationship_idx" ON "lineage_records"("relationship");

-- CreateIndex
CREATE UNIQUE INDEX "lineage_records_sourceType_sourceId_targetType_targetId_key" ON "lineage_records"("sourceType", "sourceId", "targetType", "targetId");

-- CreateIndex
CREATE INDEX "data_catalogs_category_idx" ON "data_catalogs"("category");

-- CreateIndex
CREATE UNIQUE INDEX "data_catalogs_assetType_assetId_key" ON "data_catalogs"("assetType", "assetId");

-- CreateIndex
CREATE UNIQUE INDEX "users_username_key" ON "users"("username");

-- AddForeignKey
ALTER TABLE "pipelines" ADD CONSTRAINT "pipelines_dataSourceId_fkey" FOREIGN KEY ("dataSourceId") REFERENCES "data_sources"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pipeline_executions" ADD CONSTRAINT "pipeline_executions_pipelineId_fkey" FOREIGN KEY ("pipelineId") REFERENCES "pipelines"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "data_assets" ADD CONSTRAINT "data_assets_executionId_fkey" FOREIGN KEY ("executionId") REFERENCES "pipeline_executions"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "queries" ADD CONSTRAINT "queries_assetId_fkey" FOREIGN KEY ("assetId") REFERENCES "data_assets"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "entity_relations" ADD CONSTRAINT "entity_relations_sourceId_fkey" FOREIGN KEY ("sourceId") REFERENCES "entities"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "entity_relations" ADD CONSTRAINT "entity_relations_targetId_fkey" FOREIGN KEY ("targetId") REFERENCES "entities"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lineage_records" ADD CONSTRAINT "lineage_records_executionId_fkey" FOREIGN KEY ("executionId") REFERENCES "pipeline_executions"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "lineage_records" ADD CONSTRAINT "lineage_records_entityId_fkey" FOREIGN KEY ("entityId") REFERENCES "entities"("id") ON DELETE SET NULL ON UPDATE CASCADE;
