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

#nullable disable

namespace Tiku.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class AddCommerceAdjustmentVouchers : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "commerce_adjustment_vouchers",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    issue_id = table.Column<Guid>(type: "uuid", nullable: true),
                    batch_id = table.Column<Guid>(type: "uuid", nullable: true),
                    item_id = table.Column<Guid>(type: "uuid", nullable: true),
                    order_id = table.Column<Guid>(type: "uuid", nullable: true),
                    payment_id = table.Column<Guid>(type: "uuid", nullable: true),
                    refund_request_id = table.Column<Guid>(type: "uuid", nullable: true),
                    created_by = table.Column<Guid>(type: "uuid", nullable: true),
                    reviewed_by = table.Column<Guid>(type: "uuid", nullable: true),
                    voucher_no = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    direction = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    amount_cents = table.Column<int>(type: "integer", nullable: false),
                    currency = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: false),
                    reason = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: false),
                    proof_asset_key = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    reviewed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    closed_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_commerce_adjustment_vouchers", x => x.id);
                    table.UniqueConstraint("ak_commerce_adjustment_vouchers_tenant_id_id", x => new { x.tenant_id, x.id });
                    table.CheckConstraint("ck_commerce_adjustment_vouchers_amount", "amount_cents > 0");
                    table.ForeignKey(
                        name: "fk_commerce_adjustment_vouchers_commerce_reconciliation_batche~",
                        columns: x => new { x.tenant_id, x.batch_id },
                        principalTable: "commerce_reconciliation_batches",
                        principalColumns: new[] { "tenant_id", "id" },
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "fk_commerce_adjustment_vouchers_commerce_reconciliation_issues~",
                        columns: x => new { x.tenant_id, x.issue_id },
                        principalTable: "commerce_reconciliation_issues",
                        principalColumns: new[] { "tenant_id", "id" },
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "fk_commerce_adjustment_vouchers_commerce_reconciliation_items_~",
                        columns: x => new { x.tenant_id, x.item_id },
                        principalTable: "commerce_reconciliation_items",
                        principalColumns: new[] { "tenant_id", "id" },
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "fk_commerce_adjustment_vouchers_commerce_refund_requests_tenan~",
                        columns: x => new { x.tenant_id, x.refund_request_id },
                        principalTable: "commerce_refund_requests",
                        principalColumns: new[] { "tenant_id", "id" },
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "fk_commerce_adjustment_vouchers_orders_tenant_id_order_id",
                        columns: x => new { x.tenant_id, x.order_id },
                        principalTable: "orders",
                        principalColumns: new[] { "tenant_id", "id" },
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "fk_commerce_adjustment_vouchers_payments_tenant_id_payment_id",
                        columns: x => new { x.tenant_id, x.payment_id },
                        principalTable: "payments",
                        principalColumns: new[] { "tenant_id", "id" },
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "fk_commerce_adjustment_vouchers_tenants_tenant_id",
                        column: x => x.tenant_id,
                        principalTable: "tenants",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "fk_commerce_adjustment_vouchers_users_created_by",
                        column: x => x.created_by,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "fk_commerce_adjustment_vouchers_users_reviewed_by",
                        column: x => x.reviewed_by,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.SetNull);
                });

            migrationBuilder.CreateTable(
                name: "commerce_adjustment_voucher_events",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
                    voucher_id = table.Column<Guid>(type: "uuid", nullable: false),
                    from_status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true),
                    to_status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
                    actor_user_id = table.Column<Guid>(type: "uuid", nullable: true),
                    note = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
                    details = 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_commerce_adjustment_voucher_events", x => x.id);
                    table.UniqueConstraint("ak_commerce_adjustment_voucher_events_tenant_id_id", x => new { x.tenant_id, x.id });
                    table.ForeignKey(
                        name: "fk_commerce_adjustment_voucher_events_commerce_adjustment_vouc~",
                        columns: x => new { x.tenant_id, x.voucher_id },
                        principalTable: "commerce_adjustment_vouchers",
                        principalColumns: new[] { "tenant_id", "id" },
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "fk_commerce_adjustment_voucher_events_tenants_tenant_id",
                        column: x => x.tenant_id,
                        principalTable: "tenants",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "fk_commerce_adjustment_voucher_events_users_actor_user_id",
                        column: x => x.actor_user_id,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.SetNull);
                });

            migrationBuilder.CreateIndex(
                name: "ix_commerce_adjustment_voucher_events_actor_user_id",
                table: "commerce_adjustment_voucher_events",
                column: "actor_user_id");

            migrationBuilder.CreateIndex(
                name: "ix_commerce_adjustment_voucher_events_tenant_id_voucher_id_cre~",
                table: "commerce_adjustment_voucher_events",
                columns: new[] { "tenant_id", "voucher_id", "created_at" });

            migrationBuilder.CreateIndex(
                name: "ix_commerce_adjustment_vouchers_created_by",
                table: "commerce_adjustment_vouchers",
                column: "created_by");

            migrationBuilder.CreateIndex(
                name: "ix_commerce_adjustment_vouchers_reviewed_by",
                table: "commerce_adjustment_vouchers",
                column: "reviewed_by");

            migrationBuilder.CreateIndex(
                name: "ix_commerce_adjustment_vouchers_tenant_id_batch_id",
                table: "commerce_adjustment_vouchers",
                columns: new[] { "tenant_id", "batch_id" });

            migrationBuilder.CreateIndex(
                name: "ix_commerce_adjustment_vouchers_tenant_id_issue_id_created_at",
                table: "commerce_adjustment_vouchers",
                columns: new[] { "tenant_id", "issue_id", "created_at" });

            migrationBuilder.CreateIndex(
                name: "ix_commerce_adjustment_vouchers_tenant_id_item_id",
                table: "commerce_adjustment_vouchers",
                columns: new[] { "tenant_id", "item_id" });

            migrationBuilder.CreateIndex(
                name: "ix_commerce_adjustment_vouchers_tenant_id_order_id",
                table: "commerce_adjustment_vouchers",
                columns: new[] { "tenant_id", "order_id" });

            migrationBuilder.CreateIndex(
                name: "ix_commerce_adjustment_vouchers_tenant_id_payment_id",
                table: "commerce_adjustment_vouchers",
                columns: new[] { "tenant_id", "payment_id" });

            migrationBuilder.CreateIndex(
                name: "ix_commerce_adjustment_vouchers_tenant_id_refund_request_id",
                table: "commerce_adjustment_vouchers",
                columns: new[] { "tenant_id", "refund_request_id" });

            migrationBuilder.CreateIndex(
                name: "ix_commerce_adjustment_vouchers_tenant_id_status_created_at",
                table: "commerce_adjustment_vouchers",
                columns: new[] { "tenant_id", "status", "created_at" });

            migrationBuilder.CreateIndex(
                name: "ix_commerce_adjustment_vouchers_tenant_id_voucher_no",
                table: "commerce_adjustment_vouchers",
                columns: new[] { "tenant_id", "voucher_no" },
                unique: true);
        }

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

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