using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddMemberMessageReachModule : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "member_message_templates",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Name = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "模板名称。"),
                    Category = table.Column<int>(type: "integer", nullable: false, comment: "模板分类。"),
                    Content = table.Column<string>(type: "text", nullable: false, comment: "模板内容。"),
                    UsageCount = table.Column<int>(type: "integer", nullable: false, comment: "使用次数。"),
                    LastUsedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "最近使用时间（UTC）。"),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间（UTC）。"),
                    UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "最近一次更新时间（UTC），从未更新时为 null。"),
                    DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "软删除时间（UTC），未删除时为 null。"),
                    CreatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "创建人用户标识，匿名或系统操作时为 null。"),
                    UpdatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "最后更新人用户标识，匿名或系统操作时为 null。"),
                    DeletedBy = table.Column<long>(type: "bigint", nullable: true, comment: "删除人用户标识（软删除），未删除时为 null。"),
                    TenantId = table.Column<long>(type: "bigint", nullable: false, comment: "所属租户 ID。")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_member_message_templates", x => x.Id);
                },
                comment: "会员消息模板。");

            migrationBuilder.CreateTable(
                name: "member_reach_messages",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    StoreId = table.Column<long>(type: "bigint", nullable: true, comment: "门店标识。"),
                    TemplateId = table.Column<long>(type: "bigint", nullable: true, comment: "模板标识。"),
                    Title = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false, comment: "消息标题。"),
                    Content = table.Column<string>(type: "text", nullable: false, comment: "消息内容。"),
                    ChannelsJson = table.Column<string>(type: "text", nullable: false, comment: "发送渠道 JSON。"),
                    AudienceType = table.Column<int>(type: "integer", nullable: false, comment: "目标人群类型。"),
                    AudienceTagsJson = table.Column<string>(type: "text", nullable: false, comment: "目标标签 JSON。"),
                    EstimatedReachCount = table.Column<int>(type: "integer", nullable: false, comment: "预计触达人数。"),
                    ScheduleType = table.Column<int>(type: "integer", nullable: false, comment: "发送时间类型。"),
                    ScheduledAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "定时发送时间（UTC）。"),
                    Status = table.Column<int>(type: "integer", nullable: false, comment: "消息状态。"),
                    SentAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "发送时间（UTC）。"),
                    SentCount = table.Column<int>(type: "integer", nullable: false, comment: "发送成功数量。"),
                    ReadCount = table.Column<int>(type: "integer", nullable: false, comment: "已读数量。"),
                    ConvertedCount = table.Column<int>(type: "integer", nullable: false, comment: "转化数量。"),
                    HangfireJobId = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true, comment: "Hangfire 任务 ID。"),
                    LastError = table.Column<string>(type: "character varying(1024)", maxLength: 1024, nullable: true, comment: "最后错误信息。"),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间（UTC）。"),
                    UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "最近一次更新时间（UTC），从未更新时为 null。"),
                    DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "软删除时间（UTC），未删除时为 null。"),
                    CreatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "创建人用户标识，匿名或系统操作时为 null。"),
                    UpdatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "最后更新人用户标识，匿名或系统操作时为 null。"),
                    DeletedBy = table.Column<long>(type: "bigint", nullable: true, comment: "删除人用户标识（软删除），未删除时为 null。"),
                    TenantId = table.Column<long>(type: "bigint", nullable: false, comment: "所属租户 ID。")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_member_reach_messages", x => x.Id);
                },
                comment: "会员消息触达主记录。");

            migrationBuilder.CreateTable(
                name: "member_reach_recipients",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    MessageId = table.Column<long>(type: "bigint", nullable: false, comment: "消息标识。"),
                    MemberId = table.Column<long>(type: "bigint", nullable: false, comment: "会员标识。"),
                    Channel = table.Column<int>(type: "integer", nullable: false, comment: "触达渠道。"),
                    Mobile = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true, comment: "手机号快照。"),
                    OpenId = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true, comment: "OpenId 快照。"),
                    Status = table.Column<int>(type: "integer", nullable: false, comment: "发送状态。"),
                    SentAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "发送时间（UTC）。"),
                    ReadAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "已读时间（UTC）。"),
                    ConvertedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "转化时间（UTC）。"),
                    ErrorMessage = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true, comment: "失败摘要。"),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间（UTC）。"),
                    UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "最近一次更新时间（UTC），从未更新时为 null。"),
                    DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "软删除时间（UTC），未删除时为 null。"),
                    CreatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "创建人用户标识，匿名或系统操作时为 null。"),
                    UpdatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "最后更新人用户标识，匿名或系统操作时为 null。"),
                    DeletedBy = table.Column<long>(type: "bigint", nullable: true, comment: "删除人用户标识（软删除），未删除时为 null。"),
                    TenantId = table.Column<long>(type: "bigint", nullable: false, comment: "所属租户 ID。")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_member_reach_recipients", x => x.Id);
                },
                comment: "会员消息触达收件明细。");

            migrationBuilder.CreateIndex(
                name: "IX_member_message_templates_TenantId_Category_UsageCount",
                table: "member_message_templates",
                columns: new[] { "TenantId", "Category", "UsageCount" });

            migrationBuilder.CreateIndex(
                name: "IX_member_message_templates_TenantId_Name",
                table: "member_message_templates",
                columns: new[] { "TenantId", "Name" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_member_reach_messages_TenantId_CreatedAt",
                table: "member_reach_messages",
                columns: new[] { "TenantId", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_member_reach_messages_TenantId_Status_ScheduledAt",
                table: "member_reach_messages",
                columns: new[] { "TenantId", "Status", "ScheduledAt" });

            migrationBuilder.CreateIndex(
                name: "IX_member_reach_recipients_TenantId_MessageId_MemberId_Channel",
                table: "member_reach_recipients",
                columns: new[] { "TenantId", "MessageId", "MemberId", "Channel" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_member_reach_recipients_TenantId_MessageId_Status",
                table: "member_reach_recipients",
                columns: new[] { "TenantId", "MessageId", "Status" });
        }

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

            migrationBuilder.DropTable(
                name: "member_reach_messages");

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

