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

#nullable disable

namespace ERPCore.Migrations
{
    /// <inheritdoc />
    public partial class warrenty : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "Warranty",
                table: "items",
                type: "character varying(20)",
                maxLength: 20,
                nullable: false,
                defaultValue: "NonWarranty");

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

            migrationBuilder.CreateTable(
                name: "grn_line_warranty_numbers",
                columns: table => new
                {
                    GrnLineWarrantyNumberId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    GrnLineId = table.Column<int>(type: "integer", nullable: false),
                    WarrantyNo = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    WarrantyPeriodMonths = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_grn_line_warranty_numbers", x => x.GrnLineWarrantyNumberId);
                    table.ForeignKey(
                        name: "FK_grn_line_warranty_numbers_grn_lines_GrnLineId",
                        column: x => x.GrnLineId,
                        principalTable: "grn_lines",
                        principalColumn: "GrnLineId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "sales_returns",
                columns: table => new
                {
                    ReturnId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    DocNo = table.Column<string>(type: "character varying(30)", maxLength: 30, nullable: false),
                    CustomerId = table.Column<int>(type: "integer", nullable: false),
                    WarehouseId = table.Column<int>(type: "integer", nullable: false),
                    ReasonCodeId = table.Column<int>(type: "integer", nullable: false),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
                    CreatedBy = table.Column<int>(type: "integer", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_sales_returns", x => x.ReturnId);
                    table.ForeignKey(
                        name: "FK_sales_returns_customers_CustomerId",
                        column: x => x.CustomerId,
                        principalTable: "customers",
                        principalColumn: "CustomerId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_sales_returns_reason_codes_ReasonCodeId",
                        column: x => x.ReasonCodeId,
                        principalTable: "reason_codes",
                        principalColumn: "ReasonCodeId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_sales_returns_users_CreatedBy",
                        column: x => x.CreatedBy,
                        principalTable: "users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_sales_returns_warehouses_WarehouseId",
                        column: x => x.WarehouseId,
                        principalTable: "warehouses",
                        principalColumn: "WarehouseId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "sales_return_lines",
                columns: table => new
                {
                    ReturnLineId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    ReturnId = table.Column<int>(type: "integer", nullable: false),
                    SalesInvoiceLineId = table.Column<int>(type: "integer", nullable: true),
                    ItemId = table.Column<int>(type: "integer", nullable: false),
                    Qty = table.Column<decimal>(type: "numeric(18,4)", precision: 18, scale: 4, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_sales_return_lines", x => x.ReturnLineId);
                    table.ForeignKey(
                        name: "FK_sales_return_lines_items_ItemId",
                        column: x => x.ItemId,
                        principalTable: "items",
                        principalColumn: "ItemId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_sales_return_lines_sales_invoice_lines_SalesInvoiceLineId",
                        column: x => x.SalesInvoiceLineId,
                        principalTable: "sales_invoice_lines",
                        principalColumn: "SalesInvoiceLineId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_sales_return_lines_sales_returns_ReturnId",
                        column: x => x.ReturnId,
                        principalTable: "sales_returns",
                        principalColumn: "ReturnId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_grn_line_warranty_numbers_GrnLineId_WarrantyNo",
                table: "grn_line_warranty_numbers",
                columns: new[] { "GrnLineId", "WarrantyNo" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_sales_return_lines_ItemId",
                table: "sales_return_lines",
                column: "ItemId");

            migrationBuilder.CreateIndex(
                name: "IX_sales_return_lines_ReturnId",
                table: "sales_return_lines",
                column: "ReturnId");

            migrationBuilder.CreateIndex(
                name: "IX_sales_return_lines_SalesInvoiceLineId",
                table: "sales_return_lines",
                column: "SalesInvoiceLineId");

            migrationBuilder.CreateIndex(
                name: "IX_sales_returns_CreatedBy",
                table: "sales_returns",
                column: "CreatedBy");

            migrationBuilder.CreateIndex(
                name: "IX_sales_returns_CustomerId",
                table: "sales_returns",
                column: "CustomerId");

            migrationBuilder.CreateIndex(
                name: "IX_sales_returns_DocNo",
                table: "sales_returns",
                column: "DocNo",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_sales_returns_ReasonCodeId",
                table: "sales_returns",
                column: "ReasonCodeId");

            migrationBuilder.CreateIndex(
                name: "IX_sales_returns_WarehouseId",
                table: "sales_returns",
                column: "WarehouseId");
        }

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

            migrationBuilder.DropTable(
                name: "sales_return_lines");

            migrationBuilder.DropTable(
                name: "sales_returns");

            migrationBuilder.DropColumn(
                name: "Warranty",
                table: "items");

            migrationBuilder.DropColumn(
                name: "WarrantyPeriodMonths",
                table: "items");
        }
    }
}
