using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace TakeoutSaaS.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class AddTenantAnnouncementStatusAndPublisher : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<int>(
                name: "PublisherScope",
                table: "tenant_announcements",
                type: "integer",
                nullable: false,
                defaultValue: 0,
                comment: "发布者范围。");

            migrationBuilder.AddColumn<long>(
                name: "PublisherUserId",
                table: "tenant_announcements",
                type: "bigint",
                nullable: true,
                comment: "发布者用户 ID（平台或租户后台账号）。");

            migrationBuilder.AddColumn<int>(
                name: "Status",
                table: "tenant_announcements",
                type: "integer",
                nullable: false,
                defaultValue: 0,
                comment: "公告状态。");

            migrationBuilder.AddColumn<DateTime>(
                name: "PublishedAt",
                table: "tenant_announcements",
                type: "timestamp with time zone",
                nullable: true,
                comment: "实际发布时间（UTC）。");

            migrationBuilder.AddColumn<DateTime>(
                name: "RevokedAt",
                table: "tenant_announcements",
                type: "timestamp with time zone",
                nullable: true,
                comment: "撤销时间（UTC）。");

            migrationBuilder.AddColumn<DateTime>(
                name: "ScheduledPublishAt",
                table: "tenant_announcements",
                type: "timestamp with time zone",
                nullable: true,
                comment: "预定发布时间（UTC）。");

            migrationBuilder.AddColumn<string>(
                name: "TargetType",
                table: "tenant_announcements",
                type: "character varying(64)",
                maxLength: 64,
                nullable: false,
                defaultValue: "",
                comment: "目标受众类型。");

            migrationBuilder.AddColumn<string>(
                name: "TargetParameters",
                table: "tenant_announcements",
                type: "text",
                nullable: true,
                comment: "目标受众参数（JSON）。");

            migrationBuilder.AddColumn<byte[]>(
                name: "RowVersion",
                table: "tenant_announcements",
                type: "bytea",
                rowVersion: true,
                nullable: false,
                defaultValue: new byte[0],
                comment: "并发控制字段。");

            migrationBuilder.Sql(
                "UPDATE tenant_announcements SET \"Status\" = CASE WHEN \"IsActive\" THEN 1 ELSE 0 END;");

            migrationBuilder.CreateIndex(
                name: "IX_tenant_announcements_TenantId_Status_EffectiveFrom",
                table: "tenant_announcements",
                columns: new[] { "TenantId", "Status", "EffectiveFrom" });

            migrationBuilder.CreateIndex(
                name: "IX_tenant_announcements_Status_EffectiveFrom_Platform",
                table: "tenant_announcements",
                columns: new[] { "Status", "EffectiveFrom" },
                filter: "\"TenantId\" = 0");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropIndex(
                name: "IX_tenant_announcements_TenantId_Status_EffectiveFrom",
                table: "tenant_announcements");

            migrationBuilder.DropIndex(
                name: "IX_tenant_announcements_Status_EffectiveFrom_Platform",
                table: "tenant_announcements");

            migrationBuilder.DropColumn(
                name: "PublisherScope",
                table: "tenant_announcements");

            migrationBuilder.DropColumn(
                name: "PublisherUserId",
                table: "tenant_announcements");

            migrationBuilder.DropColumn(
                name: "Status",
                table: "tenant_announcements");

            migrationBuilder.DropColumn(
                name: "PublishedAt",
                table: "tenant_announcements");

            migrationBuilder.DropColumn(
                name: "RevokedAt",
                table: "tenant_announcements");

            migrationBuilder.DropColumn(
                name: "ScheduledPublishAt",
                table: "tenant_announcements");

            migrationBuilder.DropColumn(
                name: "TargetType",
                table: "tenant_announcements");

            migrationBuilder.DropColumn(
                name: "TargetParameters",
                table: "tenant_announcements");

            migrationBuilder.DropColumn(
                name: "RowVersion",
                table: "tenant_announcements");
        }
    }
}
