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

#nullable disable

#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional

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

            migrationBuilder.CreateTable(
                name: "nav_items",
                columns: table => new
                {
                    NavItemId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Code = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    Label = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    Icon = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
                    Href = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
                    SortOrder = table.Column<int>(type: "integer", nullable: false),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, defaultValue: "Active")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_nav_items", x => x.NavItemId);
                });

            migrationBuilder.CreateTable(
                name: "roles",
                columns: table => new
                {
                    RoleId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    auth_role_id = table.Column<Guid>(type: "uuid", nullable: false),
                    Code = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    IsSystemRole = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, defaultValue: "Active"),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    xmin = table.Column<uint>(type: "xid", rowVersion: true, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_roles", x => x.RoleId);
                });

            migrationBuilder.CreateTable(
                name: "sub_nav_items",
                columns: table => new
                {
                    SubNavItemId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    NavItemId = table.Column<int>(type: "integer", nullable: false),
                    Code = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    Label = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    Icon = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
                    Href = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
                    SortOrder = table.Column<int>(type: "integer", nullable: false),
                    Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, defaultValue: "Active")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_sub_nav_items", x => x.SubNavItemId);
                    table.ForeignKey(
                        name: "FK_sub_nav_items_nav_items_NavItemId",
                        column: x => x.NavItemId,
                        principalTable: "nav_items",
                        principalColumn: "NavItemId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "permissions",
                columns: table => new
                {
                    PermissionId = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Code = table.Column<string>(type: "character varying(80)", maxLength: 80, nullable: false),
                    NavItemId = table.Column<int>(type: "integer", nullable: true),
                    SubNavItemId = table.Column<int>(type: "integer", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_permissions", x => x.PermissionId);
                    table.ForeignKey(
                        name: "FK_permissions_nav_items_NavItemId",
                        column: x => x.NavItemId,
                        principalTable: "nav_items",
                        principalColumn: "NavItemId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_permissions_sub_nav_items_SubNavItemId",
                        column: x => x.SubNavItemId,
                        principalTable: "sub_nav_items",
                        principalColumn: "SubNavItemId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "role_permissions",
                columns: table => new
                {
                    RoleId = table.Column<int>(type: "integer", nullable: false),
                    PermissionId = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_role_permissions", x => new { x.RoleId, x.PermissionId });
                    table.ForeignKey(
                        name: "FK_role_permissions_permissions_PermissionId",
                        column: x => x.PermissionId,
                        principalTable: "permissions",
                        principalColumn: "PermissionId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_role_permissions_roles_RoleId",
                        column: x => x.RoleId,
                        principalTable: "roles",
                        principalColumn: "RoleId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.InsertData(
                table: "nav_items",
                columns: new[] { "NavItemId", "Code", "Href", "Icon", "Label", "SortOrder" },
                values: new object[,]
                {
                    { 1, "dashboard", "/dashboard", null, "Dashboard", 1 },
                    { 2, "products", "/dashboard/products", null, "Products", 2 },
                    { 3, "vendors", "/dashboard/vendors", null, "Vendors", 3 },
                    { 4, "procurement", "/dashboard/procurement", null, "Procurement", 4 },
                    { 5, "receiving", "/dashboard/receiving/grn", null, "Receiving", 5 },
                    { 6, "stock", "/dashboard/stock", null, "Stock", 6 },
                    { 7, "warehouses", "/dashboard/warehouse", null, "Warehouses", 7 },
                    { 8, "orders", "/dashboard/orders", null, "Orders", 8 },
                    { 9, "settings", "/dashboard/settings", null, "Settings", 9 },
                    { 10, "help", "/dashboard/help", null, "Help", 10 }
                });

            migrationBuilder.UpdateData(
                table: "users",
                keyColumn: "UserId",
                keyValue: 1,
                column: "RoleId",
                value: null);

            migrationBuilder.InsertData(
                table: "permissions",
                columns: new[] { "PermissionId", "Code", "NavItemId", "SubNavItemId" },
                values: new object[,]
                {
                    { 1, "NAV:dashboard", 1, null },
                    { 2, "NAV:products", 2, null },
                    { 3, "NAV:vendors", 3, null },
                    { 4, "NAV:procurement", 4, null },
                    { 5, "NAV:receiving", 5, null },
                    { 6, "NAV:stock", 6, null },
                    { 7, "NAV:warehouses", 7, null },
                    { 8, "NAV:orders", 8, null },
                    { 9, "NAV:settings", 9, null },
                    { 10, "NAV:help", 10, null }
                });

            migrationBuilder.InsertData(
                table: "sub_nav_items",
                columns: new[] { "SubNavItemId", "Code", "Href", "Icon", "Label", "NavItemId", "SortOrder" },
                values: new object[,]
                {
                    { 1, "products.item", "/dashboard/products", null, "Item", 2, 1 },
                    { 2, "products.category", "/dashboard/products/categories", null, "Category", 2, 2 },
                    { 3, "products.brand", "/dashboard/products/brands", null, "Brand", 2, 3 },
                    { 4, "products.item-type", "/dashboard/products/item-types", null, "Item Type", 2, 4 },
                    { 5, "products.uom", "/dashboard/products/uoms", null, "UOM", 2, 5 },
                    { 6, "products.configuration", "/dashboard/products/settings", null, "Configuration", 2, 6 },
                    { 7, "settings.roles", "/dashboard/settings/roles", null, "Roles", 9, 1 },
                    { 8, "settings.users", "/dashboard/settings/users", null, "Users", 9, 2 }
                });

            migrationBuilder.InsertData(
                table: "permissions",
                columns: new[] { "PermissionId", "Code", "NavItemId", "SubNavItemId" },
                values: new object[,]
                {
                    { 11, "NAV:products.item", null, 1 },
                    { 12, "NAV:products.category", null, 2 },
                    { 13, "NAV:products.brand", null, 3 },
                    { 14, "NAV:products.item-type", null, 4 },
                    { 15, "NAV:products.uom", null, 5 },
                    { 16, "NAV:products.configuration", null, 6 },
                    { 17, "NAV:settings.roles", null, 7 },
                    { 18, "NAV:settings.users", null, 8 }
                });

            migrationBuilder.CreateIndex(
                name: "IX_users_RoleId",
                table: "users",
                column: "RoleId");

            migrationBuilder.CreateIndex(
                name: "IX_nav_items_Code",
                table: "nav_items",
                column: "Code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_permissions_Code",
                table: "permissions",
                column: "Code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_permissions_NavItemId",
                table: "permissions",
                column: "NavItemId");

            migrationBuilder.CreateIndex(
                name: "IX_permissions_SubNavItemId",
                table: "permissions",
                column: "SubNavItemId");

            migrationBuilder.CreateIndex(
                name: "IX_role_permissions_PermissionId",
                table: "role_permissions",
                column: "PermissionId");

            migrationBuilder.CreateIndex(
                name: "IX_roles_auth_role_id",
                table: "roles",
                column: "auth_role_id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_roles_Code",
                table: "roles",
                column: "Code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_sub_nav_items_Code",
                table: "sub_nav_items",
                column: "Code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_sub_nav_items_NavItemId",
                table: "sub_nav_items",
                column: "NavItemId");

            migrationBuilder.AddForeignKey(
                name: "FK_users_roles_RoleId",
                table: "users",
                column: "RoleId",
                principalTable: "roles",
                principalColumn: "RoleId",
                onDelete: ReferentialAction.Restrict);
        }

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

            migrationBuilder.DropTable(
                name: "role_permissions");

            migrationBuilder.DropTable(
                name: "permissions");

            migrationBuilder.DropTable(
                name: "roles");

            migrationBuilder.DropTable(
                name: "sub_nav_items");

            migrationBuilder.DropTable(
                name: "nav_items");

            migrationBuilder.DropIndex(
                name: "IX_users_RoleId",
                table: "users");

            migrationBuilder.DropColumn(
                name: "RoleId",
                table: "users");
        }
    }
}
