using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace CMSMicroservice.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class AddVATSystem : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<bool>(
                name: "HasVAT",
                schema: "CMS",
                table: "UserOrders",
                type: "bit",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<long>(
                name: "OrderVATId",
                schema: "CMS",
                table: "UserOrders",
                type: "bigint",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "OrderVATs",
                schema: "CMS",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    OrderId = table.Column<long>(type: "bigint", nullable: false),
                    VATRate = table.Column<decimal>(type: "decimal(5,4)", nullable: false),
                    BaseAmount = table.Column<long>(type: "bigint", nullable: false),
                    VATAmount = table.Column<long>(type: "bigint", nullable: false),
                    TotalAmount = table.Column<long>(type: "bigint", nullable: false),
                    IsPaid = table.Column<bool>(type: "bit", nullable: false, defaultValue: false),
                    PaidAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    Note = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
                    Created = table.Column<DateTime>(type: "datetime2", nullable: false),
                    CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    LastModified = table.Column<DateTime>(type: "datetime2", nullable: true),
                    LastModifiedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OrderVATs", x => x.Id);
                    table.ForeignKey(
                        name: "FK_OrderVATs_UserOrders_OrderId",
                        column: x => x.OrderId,
                        principalSchema: "CMS",
                        principalTable: "UserOrders",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_UserOrders_OrderVATId",
                schema: "CMS",
                table: "UserOrders",
                column: "OrderVATId");

            migrationBuilder.CreateIndex(
                name: "IX_OrderVATs_Created",
                schema: "CMS",
                table: "OrderVATs",
                column: "Created");

            migrationBuilder.CreateIndex(
                name: "IX_OrderVATs_IsPaid",
                schema: "CMS",
                table: "OrderVATs",
                column: "IsPaid");

            migrationBuilder.CreateIndex(
                name: "IX_OrderVATs_OrderId",
                schema: "CMS",
                table: "OrderVATs",
                column: "OrderId",
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_UserOrders_OrderVATs_OrderVATId",
                schema: "CMS",
                table: "UserOrders",
                column: "OrderVATId",
                principalSchema: "CMS",
                principalTable: "OrderVATs",
                principalColumn: "Id");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_UserOrders_OrderVATs_OrderVATId",
                schema: "CMS",
                table: "UserOrders");

            migrationBuilder.DropTable(
                name: "OrderVATs",
                schema: "CMS");

            migrationBuilder.DropIndex(
                name: "IX_UserOrders_OrderVATId",
                schema: "CMS",
                table: "UserOrders");

            migrationBuilder.DropColumn(
                name: "HasVAT",
                schema: "CMS",
                table: "UserOrders");

            migrationBuilder.DropColumn(
                name: "OrderVATId",
                schema: "CMS",
                table: "UserOrders");
        }
    }
}
