﻿CREATE TABLE IF NOT EXISTS "__EFMigrationsHistory" (
    "MigrationId" character varying(150) NOT NULL,
    "ProductVersion" character varying(32) NOT NULL,
    CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId")
);

START TRANSACTION;
CREATE TABLE "AccessLogs" (
    "Id" bigint GENERATED BY DEFAULT AS IDENTITY,
    "UserName" character varying(50),
    "TenantId" character varying(50),
    "Action" character varying(20) NOT NULL,
    "Resource" character varying(200),
    "Method" character varying(10),
    "IpAddress" character varying(50),
    "UserAgent" character varying(500),
    "Status" character varying(20) NOT NULL,
    "Duration" integer NOT NULL,
    "RequestData" text,
    "ResponseData" text,
    "ErrorMessage" text,
    "CreatedAt" timestamp with time zone NOT NULL,
    CONSTRAINT "PK_AccessLogs" PRIMARY KEY ("Id")
);

CREATE TABLE "AspNetRoles" (
    "Id" bigint GENERATED BY DEFAULT AS IDENTITY,
    "Description" character varying(200),
    "CreatedTime" timestamp with time zone NOT NULL,
    "TenantId" bigint,
    "IsSystem" boolean NOT NULL,
    "DisplayName" text,
    "Permissions" text[],
    "Name" character varying(256),
    "NormalizedName" character varying(256),
    "ConcurrencyStamp" text,
    CONSTRAINT "PK_AspNetRoles" PRIMARY KEY ("Id")
);

CREATE TABLE "AspNetUsers" (
    "Id" bigint GENERATED BY DEFAULT AS IDENTITY,
    "RealName" text NOT NULL,
    "TenantId" bigint,
    "TenantCode" text,
    "TenantName" text,
    "CreatedTime" timestamp with time zone NOT NULL,
    "UpdatedTime" timestamp with time zone,
    "IsDeleted" boolean NOT NULL,
    "UserName" character varying(256),
    "NormalizedUserName" character varying(256),
    "Email" character varying(256),
    "NormalizedEmail" character varying(256),
    "EmailConfirmed" boolean NOT NULL,
    "PasswordHash" text,
    "SecurityStamp" text,
    "ConcurrencyStamp" text,
    "PhoneNumber" character varying(20),
    "PhoneNumberConfirmed" boolean NOT NULL,
    "TwoFactorEnabled" boolean NOT NULL,
    "LockoutEnd" timestamp with time zone,
    "LockoutEnabled" boolean NOT NULL,
    "AccessFailedCount" integer NOT NULL,
    CONSTRAINT "PK_AspNetUsers" PRIMARY KEY ("Id")
);

CREATE TABLE "AuditLogs" (
    "Id" bigint GENERATED BY DEFAULT AS IDENTITY,
    "Operator" character varying(50) NOT NULL,
    "TenantId" character varying(50),
    "Operation" character varying(20) NOT NULL,
    "Action" character varying(20) NOT NULL,
    "TargetType" character varying(50),
    "TargetId" bigint,
    "TargetName" character varying(100),
    "IpAddress" character varying(50) NOT NULL,
    "Description" character varying(500),
    "OldValue" text,
    "NewValue" text,
    "ErrorMessage" text,
    "Status" character varying(20) NOT NULL,
    "CreatedAt" timestamp with time zone NOT NULL,
    CONSTRAINT "PK_AuditLogs" PRIMARY KEY ("Id")
);

CREATE TABLE "Platform_Tenants" (
    "Id" bigint GENERATED BY DEFAULT AS IDENTITY,
    "TenantCode" character varying(50) NOT NULL,
    "Name" character varying(100) NOT NULL,
    "ContactName" character varying(50) NOT NULL,
    "ContactEmail" character varying(100) NOT NULL,
    "ContactPhone" character varying(20),
    "MaxUsers" integer,
    "CreatedAt" timestamp with time zone NOT NULL,
    "UpdatedAt" timestamp with time zone,
    "ExpiresAt" timestamp with time zone,
    "Description" character varying(500),
    "Status" integer NOT NULL,
    "IsDeleted" boolean NOT NULL,
    "RowVersion" bigint NOT NULL,
    CONSTRAINT "PK_Platform_Tenants" PRIMARY KEY ("Id")
);

CREATE TABLE "AspNetRoleClaims" (
    "Id" integer GENERATED BY DEFAULT AS IDENTITY,
    "RoleId" bigint NOT NULL,
    "ClaimType" text,
    "ClaimValue" text,
    CONSTRAINT "PK_AspNetRoleClaims" PRIMARY KEY ("Id"),
    CONSTRAINT "FK_AspNetRoleClaims_AspNetRoles_RoleId" FOREIGN KEY ("RoleId") REFERENCES "AspNetRoles" ("Id") ON DELETE CASCADE
);

CREATE TABLE "AspNetUserClaims" (
    "Id" integer GENERATED BY DEFAULT AS IDENTITY,
    "UserId" bigint NOT NULL,
    "ClaimType" text,
    "ClaimValue" text,
    CONSTRAINT "PK_AspNetUserClaims" PRIMARY KEY ("Id"),
    CONSTRAINT "FK_AspNetUserClaims_AspNetUsers_UserId" FOREIGN KEY ("UserId") REFERENCES "AspNetUsers" ("Id") ON DELETE CASCADE
);

CREATE TABLE "AspNetUserLogins" (
    "LoginProvider" text NOT NULL,
    "ProviderKey" text NOT NULL,
    "ProviderDisplayName" text,
    "UserId" bigint NOT NULL,
    CONSTRAINT "PK_AspNetUserLogins" PRIMARY KEY ("LoginProvider", "ProviderKey"),
    CONSTRAINT "FK_AspNetUserLogins_AspNetUsers_UserId" FOREIGN KEY ("UserId") REFERENCES "AspNetUsers" ("Id") ON DELETE CASCADE
);

CREATE TABLE "AspNetUserRoles" (
    "UserId" bigint NOT NULL,
    "RoleId" bigint NOT NULL,
    CONSTRAINT "PK_AspNetUserRoles" PRIMARY KEY ("UserId", "RoleId"),
    CONSTRAINT "FK_AspNetUserRoles_AspNetRoles_RoleId" FOREIGN KEY ("RoleId") REFERENCES "AspNetRoles" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_AspNetUserRoles_AspNetUsers_UserId" FOREIGN KEY ("UserId") REFERENCES "AspNetUsers" ("Id") ON DELETE CASCADE
);

CREATE TABLE "AspNetUserTokens" (
    "UserId" bigint NOT NULL,
    "LoginProvider" text NOT NULL,
    "Name" text NOT NULL,
    "Value" text,
    CONSTRAINT "PK_AspNetUserTokens" PRIMARY KEY ("UserId", "LoginProvider", "Name"),
    CONSTRAINT "FK_AspNetUserTokens_AspNetUsers_UserId" FOREIGN KEY ("UserId") REFERENCES "AspNetUsers" ("Id") ON DELETE CASCADE
);

CREATE INDEX "IX_AccessLogs_Action" ON "AccessLogs" ("Action");

CREATE INDEX "IX_AccessLogs_CreatedAt" ON "AccessLogs" ("CreatedAt");

CREATE INDEX "IX_AccessLogs_Status" ON "AccessLogs" ("Status");

CREATE INDEX "IX_AccessLogs_TenantId" ON "AccessLogs" ("TenantId");

CREATE INDEX "IX_AccessLogs_UserName" ON "AccessLogs" ("UserName");

CREATE INDEX "IX_AspNetRoleClaims_RoleId" ON "AspNetRoleClaims" ("RoleId");

CREATE UNIQUE INDEX "RoleNameIndex" ON "AspNetRoles" ("NormalizedName");

CREATE INDEX "IX_AspNetUserClaims_UserId" ON "AspNetUserClaims" ("UserId");

CREATE INDEX "IX_AspNetUserLogins_UserId" ON "AspNetUserLogins" ("UserId");

CREATE INDEX "IX_AspNetUserRoles_RoleId" ON "AspNetUserRoles" ("RoleId");

CREATE INDEX "EmailIndex" ON "AspNetUsers" ("NormalizedEmail");

CREATE UNIQUE INDEX "IX_AspNetUsers_PhoneNumber" ON "AspNetUsers" ("PhoneNumber");

CREATE UNIQUE INDEX "UserNameIndex" ON "AspNetUsers" ("NormalizedUserName");

CREATE INDEX "IX_AuditLogs_Action" ON "AuditLogs" ("Action");

CREATE INDEX "IX_AuditLogs_CreatedAt" ON "AuditLogs" ("CreatedAt");

CREATE INDEX "IX_AuditLogs_Operation" ON "AuditLogs" ("Operation");

CREATE INDEX "IX_AuditLogs_Operator" ON "AuditLogs" ("Operator");

CREATE INDEX "IX_AuditLogs_TenantId" ON "AuditLogs" ("TenantId");

CREATE INDEX "IX_Platform_Tenants_Status" ON "Platform_Tenants" ("Status");

CREATE UNIQUE INDEX "IX_Platform_Tenants_TenantCode" ON "Platform_Tenants" ("TenantCode");

INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20260221065049_Initial', '10.0.0');

COMMIT;

