using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace FSI.BT.IR.Plc.Settings.Log.Data.Migrations
{
    /// <inheritdoc />
    public partial class InitialCreate : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Logging",
                columns: table => new
                {
                    Id = table.Column<int>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    ParameterId = table.Column<int>(type: "INTEGER", nullable: false),
                    Value = table.Column<string>(type: "TEXT", nullable: false),
                    Timestamp = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Logging", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "Organizations",
                columns: table => new
                {
                    Id = table.Column<int>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    Name = table.Column<string>(type: "TEXT", maxLength: 30, nullable: false),
                    ShortName = table.Column<string>(type: "TEXT", maxLength: 10, nullable: false),
                    Description = table.Column<string>(type: "TEXT", maxLength: 255, nullable: true),
                    ParentId = table.Column<int>(type: "INTEGER", nullable: true),
                    Type = table.Column<int>(type: "INTEGER", nullable: false),
                    Created = table.Column<DateTime>(type: "TEXT", nullable: false),
                    Updated = table.Column<DateTime>(type: "TEXT", nullable: false),
                    FullShortName = table.Column<string>(type: "TEXT", nullable: false),
                    ParameterId = table.Column<int>(type: "INTEGER", nullable: true),
                    PlcId = table.Column<int>(type: "INTEGER", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Organizations", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "Parameters",
                columns: table => new
                {
                    Id = table.Column<int>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    Name = table.Column<string>(type: "TEXT", nullable: false),
                    Description = table.Column<string>(type: "TEXT", nullable: true),
                    OrganizationId = table.Column<int>(type: "INTEGER", nullable: false),
                    PlcId = table.Column<int>(type: "INTEGER", nullable: false),
                    DbNo = table.Column<int>(type: "INTEGER", nullable: false),
                    DbStart = table.Column<int>(type: "INTEGER", nullable: false),
                    DbSize = table.Column<int>(type: "INTEGER", nullable: false),
                    DataType = table.Column<int>(type: "INTEGER", nullable: false),
                    DefaultValue = table.Column<string>(type: "TEXT", nullable: false),
                    Disabled = table.Column<bool>(type: "INTEGER", nullable: false),
                    Created = table.Column<DateTime>(type: "TEXT", nullable: false),
                    Updated = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Parameters", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Parameters_Organizations_OrganizationId",
                        column: x => x.OrganizationId,
                        principalTable: "Organizations",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "Plcs",
                columns: table => new
                {
                    Id = table.Column<int>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    Name = table.Column<string>(type: "TEXT", nullable: false),
                    Description = table.Column<string>(type: "TEXT", nullable: true),
                    OrganizationId = table.Column<int>(type: "INTEGER", nullable: false),
                    IpAdress = table.Column<string>(type: "TEXT", nullable: false),
                    Rack = table.Column<int>(type: "INTEGER", nullable: false),
                    Slot = table.Column<int>(type: "INTEGER", nullable: false),
                    Disabled = table.Column<bool>(type: "INTEGER", nullable: false),
                    Created = table.Column<DateTime>(type: "TEXT", nullable: false),
                    Updated = table.Column<DateTime>(type: "TEXT", nullable: false),
                    ParameterId = table.Column<int>(type: "INTEGER", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Plcs", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Plcs_Organizations_OrganizationId",
                        column: x => x.OrganizationId,
                        principalTable: "Organizations",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_Plcs_Parameters_ParameterId",
                        column: x => x.ParameterId,
                        principalTable: "Parameters",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateIndex(
                name: "IX_Logging_ParameterId",
                table: "Logging",
                column: "ParameterId");

            migrationBuilder.CreateIndex(
                name: "IX_Organizations_ParameterId",
                table: "Organizations",
                column: "ParameterId");

            migrationBuilder.CreateIndex(
                name: "IX_Organizations_PlcId",
                table: "Organizations",
                column: "PlcId");

            migrationBuilder.CreateIndex(
                name: "IX_Parameters_OrganizationId",
                table: "Parameters",
                column: "OrganizationId");

            migrationBuilder.CreateIndex(
                name: "IX_Parameters_PlcId",
                table: "Parameters",
                column: "PlcId");

            migrationBuilder.CreateIndex(
                name: "IX_Plcs_OrganizationId",
                table: "Plcs",
                column: "OrganizationId");

            migrationBuilder.CreateIndex(
                name: "IX_Plcs_ParameterId",
                table: "Plcs",
                column: "ParameterId");

            migrationBuilder.AddForeignKey(
                name: "FK_Logging_Parameters_ParameterId",
                table: "Logging",
                column: "ParameterId",
                principalTable: "Parameters",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_Organizations_Parameters_ParameterId",
                table: "Organizations",
                column: "ParameterId",
                principalTable: "Parameters",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_Organizations_Plcs_PlcId",
                table: "Organizations",
                column: "PlcId",
                principalTable: "Plcs",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_Parameters_Plcs_PlcId",
                table: "Parameters",
                column: "PlcId",
                principalTable: "Plcs",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

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

            migrationBuilder.DropForeignKey(
                name: "FK_Plcs_Parameters_ParameterId",
                table: "Plcs");

            migrationBuilder.DropForeignKey(
                name: "FK_Organizations_Plcs_PlcId",
                table: "Organizations");

            migrationBuilder.DropTable(
                name: "Logging");

            migrationBuilder.DropTable(
                name: "Parameters");

            migrationBuilder.DropTable(
                name: "Plcs");

            migrationBuilder.DropTable(
                name: "Organizations");
        }
    }
}
