using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Meezi.Infrastructure.Data.Migrations
{
    /// <inheritdoc />
    public partial class DeliveryPlatformIntegration : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropIndex(
                name: "IX_Orders_CafeId",
                table: "Orders");

            migrationBuilder.AddColumn<string>(
                name: "DeliveryMetaJson",
                table: "Orders",
                type: "character varying(4000)",
                maxLength: 4000,
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "DeliveryPlatform",
                table: "Orders",
                type: "integer",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "ExternalOrderId",
                table: "Orders",
                type: "character varying(120)",
                maxLength: 120,
                nullable: true);

            migrationBuilder.AddColumn<decimal>(
                name: "PlatformCommission",
                table: "Orders",
                type: "numeric(18,2)",
                precision: 18,
                scale: 2,
                nullable: false,
                defaultValue: 0m);

            migrationBuilder.AlterColumn<decimal>(
                name: "DefaultTaxRate",
                table: "Cafes",
                type: "numeric(5,2)",
                precision: 5,
                scale: 2,
                nullable: false,
                oldClrType: typeof(decimal),
                oldType: "numeric");

            migrationBuilder.AddColumn<string>(
                name: "DigikalaVendorId",
                table: "Cafes",
                type: "character varying(100)",
                maxLength: 100,
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Tap30VendorId",
                table: "Cafes",
                type: "character varying(100)",
                maxLength: 100,
                nullable: true);

            migrationBuilder.CreateTable(
                name: "DeliveryCommissionRates",
                columns: table => new
                {
                    Id = table.Column<string>(type: "text", nullable: false),
                    Platform = table.Column<int>(type: "integer", nullable: false),
                    RatePercent = table.Column<decimal>(type: "numeric(5,2)", precision: 5, scale: 2, nullable: false),
                    IsActive = table.Column<bool>(type: "boolean", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    CafeId = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_DeliveryCommissionRates", x => x.Id);
                    table.ForeignKey(
                        name: "FK_DeliveryCommissionRates_Cafes_CafeId",
                        column: x => x.CafeId,
                        principalTable: "Cafes",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "WebhookLogs",
                columns: table => new
                {
                    Id = table.Column<string>(type: "text", nullable: false),
                    CafeId = table.Column<string>(type: "text", nullable: true),
                    Platform = table.Column<int>(type: "integer", nullable: false),
                    RawBody = table.Column<string>(type: "text", nullable: false),
                    SignatureHeader = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
                    SignatureValid = table.Column<bool>(type: "boolean", nullable: false),
                    Processed = table.Column<bool>(type: "boolean", nullable: false),
                    Success = table.Column<bool>(type: "boolean", nullable: false),
                    ErrorMessage = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
                    AttemptCount = table.Column<int>(type: "integer", nullable: false),
                    ExternalOrderId = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: true),
                    MeeziOrderId = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
                    ProcessedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_WebhookLogs", x => x.Id);
                    table.ForeignKey(
                        name: "FK_WebhookLogs_Cafes_CafeId",
                        column: x => x.CafeId,
                        principalTable: "Cafes",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateIndex(
                name: "IX_Orders_CafeId_DeliveryPlatform_ExternalOrderId",
                table: "Orders",
                columns: new[] { "CafeId", "DeliveryPlatform", "ExternalOrderId" });

            migrationBuilder.CreateIndex(
                name: "IX_DeliveryCommissionRates_CafeId_Platform",
                table: "DeliveryCommissionRates",
                columns: new[] { "CafeId", "Platform" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_WebhookLogs_CafeId",
                table: "WebhookLogs",
                column: "CafeId");

            migrationBuilder.CreateIndex(
                name: "IX_WebhookLogs_Platform_CreatedAt",
                table: "WebhookLogs",
                columns: new[] { "Platform", "CreatedAt" });
        }

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

            migrationBuilder.DropTable(
                name: "WebhookLogs");

            migrationBuilder.DropIndex(
                name: "IX_Orders_CafeId_DeliveryPlatform_ExternalOrderId",
                table: "Orders");

            migrationBuilder.DropColumn(
                name: "DeliveryMetaJson",
                table: "Orders");

            migrationBuilder.DropColumn(
                name: "DeliveryPlatform",
                table: "Orders");

            migrationBuilder.DropColumn(
                name: "ExternalOrderId",
                table: "Orders");

            migrationBuilder.DropColumn(
                name: "PlatformCommission",
                table: "Orders");

            migrationBuilder.DropColumn(
                name: "DigikalaVendorId",
                table: "Cafes");

            migrationBuilder.DropColumn(
                name: "Tap30VendorId",
                table: "Cafes");

            migrationBuilder.AlterColumn<decimal>(
                name: "DefaultTaxRate",
                table: "Cafes",
                type: "numeric",
                nullable: false,
                oldClrType: typeof(decimal),
                oldType: "numeric(5,2)",
                oldPrecision: 5,
                oldScale: 2);

            migrationBuilder.CreateIndex(
                name: "IX_Orders_CafeId",
                table: "Orders",
                column: "CafeId");
        }
    }
}
