using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

namespace MissionService.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class InitialCreate : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "frequency_types",
                columns: table => new
                {
                    id = table.Column<int>(type: "integer", nullable: false),
                    name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_frequency_types", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "mission_categories",
                columns: table => new
                {
                    id = table.Column<int>(type: "integer", nullable: false),
                    name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_mission_categories", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "mission_statuses",
                columns: table => new
                {
                    id = table.Column<int>(type: "integer", nullable: false),
                    name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_mission_statuses", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "mission_types",
                columns: table => new
                {
                    id = table.Column<int>(type: "integer", nullable: false),
                    name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_mission_types", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "missions",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    code = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
                    title_en = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    title_vi = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    description_en = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
                    description_vi = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
                    type_id = table.Column<int>(type: "integer", nullable: false),
                    category_id = table.Column<int>(type: "integer", nullable: false),
                    reward_points = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false),
                    reward_mining_boost = table.Column<decimal>(type: "numeric(5,2)", precision: 5, scale: 2, nullable: false),
                    reward_xp = table.Column<int>(type: "integer", nullable: false),
                    reward_badge_id = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
                    frequency_id = table.Column<int>(type: "integer", nullable: false),
                    max_completions = table.Column<int>(type: "integer", nullable: false),
                    start_date = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    end_date = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    status_id = table.Column<int>(type: "integer", nullable: false),
                    priority = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_missions", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "task_statuses",
                columns: table => new
                {
                    id = table.Column<int>(type: "integer", nullable: false),
                    name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_task_statuses", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "user_checkins",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    current_streak = table.Column<int>(type: "integer", nullable: false),
                    longest_streak = table.Column<int>(type: "integer", nullable: false),
                    total_checkins = table.Column<int>(type: "integer", nullable: false),
                    last_checkin_date = table.Column<DateOnly>(type: "date", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_user_checkins", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "user_rewards",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    source_id = table.Column<Guid>(type: "uuid", nullable: false),
                    type = table.Column<int>(type: "integer", nullable: false),
                    status = table.Column<int>(type: "integer", nullable: false),
                    points = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false),
                    bonus_points = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false),
                    currency = table.Column<string>(type: "character varying(10)", maxLength: 10, nullable: false, defaultValue: "MP"),
                    earned_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    claimed_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    expires_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_user_rewards", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "user_tasks",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    mission_id = table.Column<Guid>(type: "uuid", nullable: false),
                    status_id = table.Column<int>(type: "integer", nullable: false),
                    progress_current = table.Column<int>(type: "integer", nullable: false),
                    progress_target = table.Column<int>(type: "integer", nullable: false),
                    progress_updated_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    evidence_type = table.Column<int>(type: "integer", nullable: true),
                    evidence_data = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: true),
                    evidence_screenshot_url = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    evidence_video_url = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    evidence_captured_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    verification_valid = table.Column<bool>(type: "boolean", nullable: true),
                    verification_failure_reason = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    verification_method = table.Column<int>(type: "integer", nullable: true),
                    verification_by = table.Column<Guid>(type: "uuid", nullable: true),
                    verification_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    reward_claimed = table.Column<bool>(type: "boolean", nullable: false),
                    started_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    completed_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    claimed_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_user_tasks", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "mission_rules",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    rule_type = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
                    @operator = table.Column<string>(name: "operator", type: "character varying(20)", maxLength: 20, nullable: false),
                    value = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: false),
                    metadata = table.Column<string>(type: "jsonb", nullable: true),
                    mission_id = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_mission_rules", x => x.id);
                    table.ForeignKey(
                        name: "FK_mission_rules_missions_mission_id",
                        column: x => x.mission_id,
                        principalTable: "missions",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "checkin_days",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false),
                    date = table.Column<DateOnly>(type: "date", nullable: false),
                    points_earned = table.Column<decimal>(type: "numeric(18,2)", precision: 18, scale: 2, nullable: false),
                    is_milestone = table.Column<bool>(type: "boolean", nullable: false),
                    streak_on_day = table.Column<int>(type: "integer", nullable: false),
                    user_checkin_id = table.Column<Guid>(type: "uuid", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_checkin_days", x => x.id);
                    table.ForeignKey(
                        name: "FK_checkin_days_user_checkins_user_checkin_id",
                        column: x => x.user_checkin_id,
                        principalTable: "user_checkins",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.InsertData(
                table: "frequency_types",
                columns: new[] { "id", "name" },
                values: new object[,]
                {
                    { 1, "Once" },
                    { 2, "Daily" },
                    { 3, "Weekly" },
                    { 4, "Unlimited" }
                });

            migrationBuilder.InsertData(
                table: "mission_categories",
                columns: new[] { "id", "name" },
                values: new object[,]
                {
                    { 1, "Daily" },
                    { 2, "Weekly" },
                    { 3, "Special" },
                    { 4, "Onboarding" },
                    { 5, "Event" }
                });

            migrationBuilder.InsertData(
                table: "mission_statuses",
                columns: new[] { "id", "name" },
                values: new object[,]
                {
                    { 1, "Draft" },
                    { 2, "Active" },
                    { 3, "Paused" },
                    { 4, "Expired" },
                    { 5, "Archived" }
                });

            migrationBuilder.InsertData(
                table: "mission_types",
                columns: new[] { "id", "name" },
                values: new object[,]
                {
                    { 1, "Video" },
                    { 2, "Click" },
                    { 3, "Upload" },
                    { 4, "Invite" },
                    { 5, "CheckIn" },
                    { 6, "Social" }
                });

            migrationBuilder.InsertData(
                table: "task_statuses",
                columns: new[] { "id", "name" },
                values: new object[,]
                {
                    { 1, "Pending" },
                    { 2, "InProgress" },
                    { 3, "PendingVerification" },
                    { 4, "Completed" },
                    { 5, "Rejected" },
                    { 6, "Cancelled" },
                    { 7, "Expired" }
                });

            migrationBuilder.CreateIndex(
                name: "IX_checkin_days_user_checkin_id_date",
                table: "checkin_days",
                columns: new[] { "user_checkin_id", "date" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_mission_rules_mission_id",
                table: "mission_rules",
                column: "mission_id");

            migrationBuilder.CreateIndex(
                name: "IX_missions_code",
                table: "missions",
                column: "code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_user_checkins_user_id",
                table: "user_checkins",
                column: "user_id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_user_rewards_source_id",
                table: "user_rewards",
                column: "source_id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_user_rewards_status_expires_at",
                table: "user_rewards",
                columns: new[] { "status", "expires_at" });

            migrationBuilder.CreateIndex(
                name: "IX_user_rewards_user_id",
                table: "user_rewards",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_user_tasks_mission_id",
                table: "user_tasks",
                column: "mission_id");

            migrationBuilder.CreateIndex(
                name: "IX_user_tasks_user_id",
                table: "user_tasks",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_user_tasks_user_id_mission_id",
                table: "user_tasks",
                columns: new[] { "user_id", "mission_id" });
        }

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

            migrationBuilder.DropTable(
                name: "frequency_types");

            migrationBuilder.DropTable(
                name: "mission_categories");

            migrationBuilder.DropTable(
                name: "mission_rules");

            migrationBuilder.DropTable(
                name: "mission_statuses");

            migrationBuilder.DropTable(
                name: "mission_types");

            migrationBuilder.DropTable(
                name: "task_statuses");

            migrationBuilder.DropTable(
                name: "user_rewards");

            migrationBuilder.DropTable(
                name: "user_tasks");

            migrationBuilder.DropTable(
                name: "user_checkins");

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