using System;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Tiku.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class PlatformTenantCapabilities : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "platform_payment_apps",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    app_code = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    app_name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    settlement_mode = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    description = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
                    updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_platform_payment_apps", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "sms_channels",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    provider = table.Column<string>(type: "character varying(80)", maxLength: 80, nullable: false),
                    name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    signature = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    scene = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    secret_ref = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: true),
                    priority = table.Column<int>(type: "integer", nullable: false),
                    monthly_quota = table.Column<int>(type: "integer", nullable: false),
                    config_public = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
                    metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
                    tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
                    updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_sms_channels", x => x.id);
                    table.UniqueConstraint("ak_sms_channels_tenant_id_id", x => new { x.tenant_id, x.id });
                    table.CheckConstraint("ck_sms_channels_quota", "monthly_quota >= 0");
                    table.ForeignKey(
                        name: "fk_sms_channels_tenants_tenant_id",
                        column: x => x.tenant_id,
                        principalTable: "tenants",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "platform_payment_channels",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    app_id = table.Column<Guid>(type: "uuid", nullable: false),
                    provider = table.Column<string>(type: "character varying(80)", maxLength: 80, nullable: false),
                    mode = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    display_name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    secret_ref = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: true),
                    callback_path = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    priority = table.Column<int>(type: "integer", nullable: false),
                    config_public = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
                    metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
                    updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_platform_payment_channels", x => x.id);
                    table.ForeignKey(
                        name: "fk_platform_payment_channels_platform_payment_apps_app_id",
                        column: x => x.app_id,
                        principalTable: "platform_payment_apps",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "sms_templates",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    channel_id = table.Column<Guid>(type: "uuid", nullable: false),
                    code = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: false),
                    name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    audit_status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    provider_template_code = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: true),
                    content = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: false),
                    remark = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    submitted_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    approved_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    disabled_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
                    tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
                    updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_sms_templates", x => x.id);
                    table.UniqueConstraint("ak_sms_templates_tenant_id_id", x => new { x.tenant_id, x.id });
                    table.ForeignKey(
                        name: "fk_sms_templates_sms_channels_tenant_id_channel_id",
                        columns: x => new { x.tenant_id, x.channel_id },
                        principalTable: "sms_channels",
                        principalColumns: new[] { "tenant_id", "id" },
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "fk_sms_templates_tenants_tenant_id",
                        column: x => x.tenant_id,
                        principalTable: "tenants",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "sms_send_logs",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
                    channel_id = table.Column<Guid>(type: "uuid", nullable: true),
                    template_id = table.Column<Guid>(type: "uuid", nullable: true),
                    provider = table.Column<string>(type: "character varying(80)", maxLength: 80, nullable: false),
                    scene = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    phone_masked = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: true),
                    status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    provider_message_id = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
                    error_code = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: true),
                    error_message = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
                    sent_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_sms_send_logs", x => x.id);
                    table.ForeignKey(
                        name: "fk_sms_send_logs_sms_channels_tenant_id_channel_id",
                        columns: x => new { x.tenant_id, x.channel_id },
                        principalTable: "sms_channels",
                        principalColumns: new[] { "tenant_id", "id" },
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "fk_sms_send_logs_sms_templates_tenant_id_template_id",
                        columns: x => new { x.tenant_id, x.template_id },
                        principalTable: "sms_templates",
                        principalColumns: new[] { "tenant_id", "id" },
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "fk_sms_send_logs_tenants_tenant_id",
                        column: x => x.tenant_id,
                        principalTable: "tenants",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "ix_platform_payment_apps_app_code",
                table: "platform_payment_apps",
                column: "app_code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_platform_payment_apps_status_app_code",
                table: "platform_payment_apps",
                columns: new[] { "status", "app_code" });

            migrationBuilder.CreateIndex(
                name: "ix_platform_payment_channels_app_id_provider",
                table: "platform_payment_channels",
                columns: new[] { "app_id", "provider" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_platform_payment_channels_status_priority_provider",
                table: "platform_payment_channels",
                columns: new[] { "status", "priority", "provider" });

            migrationBuilder.CreateIndex(
                name: "ix_sms_channels_tenant_id_provider_scene",
                table: "sms_channels",
                columns: new[] { "tenant_id", "provider", "scene" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "ix_sms_channels_tenant_id_status_priority",
                table: "sms_channels",
                columns: new[] { "tenant_id", "status", "priority" });

            migrationBuilder.CreateIndex(
                name: "ix_sms_send_logs_tenant_id_channel_id_created_at",
                table: "sms_send_logs",
                columns: new[] { "tenant_id", "channel_id", "created_at" });

            migrationBuilder.CreateIndex(
                name: "ix_sms_send_logs_tenant_id_created_at",
                table: "sms_send_logs",
                columns: new[] { "tenant_id", "created_at" });

            migrationBuilder.CreateIndex(
                name: "ix_sms_send_logs_tenant_id_template_id",
                table: "sms_send_logs",
                columns: new[] { "tenant_id", "template_id" });

            migrationBuilder.CreateIndex(
                name: "ix_sms_templates_tenant_id_channel_id_status",
                table: "sms_templates",
                columns: new[] { "tenant_id", "channel_id", "status" });

            migrationBuilder.CreateIndex(
                name: "ix_sms_templates_tenant_id_code",
                table: "sms_templates",
                columns: new[] { "tenant_id", "code" },
                unique: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "platform_payment_channels");

            migrationBuilder.DropTable(
                name: "sms_send_logs");

            migrationBuilder.DropTable(
                name: "platform_payment_apps");

            migrationBuilder.DropTable(
                name: "sms_templates");

            migrationBuilder.DropTable(
                name: "sms_channels");
        }
    }
}
