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

#nullable disable

namespace ERPCore.Migrations
{
    /// <inheritdoc />
    public partial class AddSalesDayEnd : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<int>(
                name: "DayEndId",
                table: "sales_slips",
                type: "integer",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "sales_day_ends",
                columns: table => new
                {
                    SalesDayEndId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    DocNo = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: false),
                    CashierUserId = table.Column<int>(type: "integer", nullable: false),
                    BusinessDate = table.Column<DateOnly>(type: "date", nullable: false),
                    SlipCount = table.Column<int>(type: "integer", nullable: false),
                    Subtotal = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    DiscountTotal = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    TaxTotal = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    GrandTotal = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    CostOfGoodsSold = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false),
                    GlJournalNo = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: true),
                    GlPostedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    ClosedBy = table.Column<int>(type: "integer", nullable: false),
                    ClosedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    xmin = table.Column<uint>(type: "xid", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_sales_day_ends", x => x.SalesDayEndId);
                    table.ForeignKey(
                        name: "FK_sales_day_ends_users_CashierUserId",
                        column: x => x.CashierUserId,
                        principalTable: "users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_sales_day_ends_users_ClosedBy",
                        column: x => x.ClosedBy,
                        principalTable: "users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_sales_slips_DayEndId",
                table: "sales_slips",
                column: "DayEndId");

            migrationBuilder.CreateIndex(
                name: "IX_sales_day_ends_CashierUserId_BusinessDate",
                table: "sales_day_ends",
                columns: new[] { "CashierUserId", "BusinessDate" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_sales_day_ends_ClosedBy",
                table: "sales_day_ends",
                column: "ClosedBy");

            migrationBuilder.CreateIndex(
                name: "IX_sales_day_ends_DocNo",
                table: "sales_day_ends",
                column: "DocNo",
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_sales_slips_sales_day_ends_DayEndId",
                table: "sales_slips",
                column: "DayEndId",
                principalTable: "sales_day_ends",
                principalColumn: "SalesDayEndId",
                onDelete: ReferentialAction.Restrict);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_sales_slips_sales_day_ends_DayEndId",
                table: "sales_slips");

            migrationBuilder.DropTable(
                name: "sales_day_ends");

            migrationBuilder.DropIndex(
                name: "IX_sales_slips_DayEndId",
                table: "sales_slips");

            migrationBuilder.DropColumn(
                name: "DayEndId",
                table: "sales_slips");
        }
    }
}
