using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace DramaLing.Api.Migrations
{
    /// <inheritdoc />
    public partial class AddSentenceAnalysisCache : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "SentenceAnalysisCache",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    InputTextHash = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
                    InputText = table.Column<string>(type: "TEXT", maxLength: 1000, nullable: false),
                    CorrectedText = table.Column<string>(type: "TEXT", maxLength: 1000, nullable: true),
                    HasGrammarErrors = table.Column<bool>(type: "INTEGER", nullable: false),
                    GrammarCorrections = table.Column<string>(type: "TEXT", nullable: true),
                    AnalysisResult = table.Column<string>(type: "TEXT", nullable: false),
                    HighValueWords = table.Column<string>(type: "TEXT", nullable: true),
                    PhrasesDetected = table.Column<string>(type: "TEXT", nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    ExpiresAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    AccessCount = table.Column<int>(type: "INTEGER", nullable: false),
                    LastAccessedAt = table.Column<DateTime>(type: "TEXT", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_SentenceAnalysisCache", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "user_profiles",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    username = table.Column<string>(type: "TEXT", maxLength: 50, nullable: false),
                    email = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
                    password_hash = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
                    display_name = table.Column<string>(type: "TEXT", maxLength: 100, nullable: true),
                    avatar_url = table.Column<string>(type: "TEXT", nullable: true),
                    subscription_type = table.Column<string>(type: "TEXT", maxLength: 20, nullable: false),
                    preferences = table.Column<string>(type: "TEXT", nullable: false),
                    created_at = table.Column<DateTime>(type: "TEXT", nullable: false),
                    updated_at = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_user_profiles", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "card_sets",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    UserId = table.Column<Guid>(type: "TEXT", nullable: false),
                    Name = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
                    Description = table.Column<string>(type: "TEXT", nullable: true),
                    Color = table.Column<string>(type: "TEXT", maxLength: 50, nullable: false),
                    CardCount = table.Column<int>(type: "INTEGER", nullable: false),
                    IsDefault = table.Column<bool>(type: "INTEGER", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_card_sets", x => x.Id);
                    table.ForeignKey(
                        name: "FK_card_sets_user_profiles_UserId",
                        column: x => x.UserId,
                        principalTable: "user_profiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "daily_stats",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    user_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    Date = table.Column<DateOnly>(type: "TEXT", nullable: false),
                    words_studied = table.Column<int>(type: "INTEGER", nullable: false),
                    words_correct = table.Column<int>(type: "INTEGER", nullable: false),
                    study_time_seconds = table.Column<int>(type: "INTEGER", nullable: false),
                    session_count = table.Column<int>(type: "INTEGER", nullable: false),
                    cards_generated = table.Column<int>(type: "INTEGER", nullable: false),
                    ai_api_calls = table.Column<int>(type: "INTEGER", nullable: false),
                    created_at = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_daily_stats", x => x.Id);
                    table.ForeignKey(
                        name: "FK_daily_stats_user_profiles_user_id",
                        column: x => x.user_id,
                        principalTable: "user_profiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "study_sessions",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    user_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    session_type = table.Column<string>(type: "TEXT", maxLength: 50, nullable: false),
                    started_at = table.Column<DateTime>(type: "TEXT", nullable: false),
                    ended_at = table.Column<DateTime>(type: "TEXT", nullable: true),
                    total_cards = table.Column<int>(type: "INTEGER", nullable: false),
                    correct_count = table.Column<int>(type: "INTEGER", nullable: false),
                    duration_seconds = table.Column<int>(type: "INTEGER", nullable: false),
                    average_response_time_ms = table.Column<int>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_study_sessions", x => x.Id);
                    table.ForeignKey(
                        name: "FK_study_sessions_user_profiles_user_id",
                        column: x => x.user_id,
                        principalTable: "user_profiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "tags",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    user_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    Name = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
                    Color = table.Column<string>(type: "TEXT", maxLength: 50, nullable: false),
                    usage_count = table.Column<int>(type: "INTEGER", nullable: false),
                    created_at = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_tags", x => x.Id);
                    table.ForeignKey(
                        name: "FK_tags_user_profiles_user_id",
                        column: x => x.user_id,
                        principalTable: "user_profiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "user_settings",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    UserId = table.Column<Guid>(type: "TEXT", nullable: false),
                    DailyGoal = table.Column<int>(type: "INTEGER", nullable: false),
                    ReminderTime = table.Column<TimeOnly>(type: "TEXT", nullable: false),
                    ReminderEnabled = table.Column<bool>(type: "INTEGER", nullable: false),
                    DifficultyPreference = table.Column<string>(type: "TEXT", maxLength: 20, nullable: false),
                    AutoPlayAudio = table.Column<bool>(type: "INTEGER", nullable: false),
                    ShowPronunciation = table.Column<bool>(type: "INTEGER", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_user_settings", x => x.Id);
                    table.ForeignKey(
                        name: "FK_user_settings_user_profiles_UserId",
                        column: x => x.UserId,
                        principalTable: "user_profiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "flashcards",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    user_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    card_set_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    Word = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
                    Translation = table.Column<string>(type: "TEXT", nullable: false),
                    Definition = table.Column<string>(type: "TEXT", nullable: false),
                    part_of_speech = table.Column<string>(type: "TEXT", maxLength: 50, nullable: true),
                    Pronunciation = table.Column<string>(type: "TEXT", maxLength: 255, nullable: true),
                    Example = table.Column<string>(type: "TEXT", nullable: true),
                    example_translation = table.Column<string>(type: "TEXT", nullable: true),
                    easiness_factor = table.Column<float>(type: "REAL", nullable: false),
                    Repetitions = table.Column<int>(type: "INTEGER", nullable: false),
                    interval_days = table.Column<int>(type: "INTEGER", nullable: false),
                    next_review_date = table.Column<DateTime>(type: "TEXT", nullable: false),
                    mastery_level = table.Column<int>(type: "INTEGER", nullable: false),
                    times_reviewed = table.Column<int>(type: "INTEGER", nullable: false),
                    times_correct = table.Column<int>(type: "INTEGER", nullable: false),
                    last_reviewed_at = table.Column<DateTime>(type: "TEXT", nullable: true),
                    is_favorite = table.Column<bool>(type: "INTEGER", nullable: false),
                    is_archived = table.Column<bool>(type: "INTEGER", nullable: false),
                    difficulty_level = table.Column<string>(type: "TEXT", maxLength: 10, nullable: true),
                    created_at = table.Column<DateTime>(type: "TEXT", nullable: false),
                    updated_at = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_flashcards", x => x.Id);
                    table.ForeignKey(
                        name: "FK_flashcards_card_sets_card_set_id",
                        column: x => x.card_set_id,
                        principalTable: "card_sets",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_flashcards_user_profiles_user_id",
                        column: x => x.user_id,
                        principalTable: "user_profiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "error_reports",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    user_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    flashcard_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    report_type = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
                    Description = table.Column<string>(type: "TEXT", nullable: true),
                    study_mode = table.Column<string>(type: "TEXT", maxLength: 50, nullable: true),
                    Status = table.Column<string>(type: "TEXT", maxLength: 50, nullable: false),
                    admin_notes = table.Column<string>(type: "TEXT", nullable: true),
                    resolved_at = table.Column<DateTime>(type: "TEXT", nullable: true),
                    resolved_by = table.Column<Guid>(type: "TEXT", nullable: true),
                    created_at = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_error_reports", x => x.Id);
                    table.ForeignKey(
                        name: "FK_error_reports_flashcards_flashcard_id",
                        column: x => x.flashcard_id,
                        principalTable: "flashcards",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_error_reports_user_profiles_resolved_by",
                        column: x => x.resolved_by,
                        principalTable: "user_profiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "FK_error_reports_user_profiles_user_id",
                        column: x => x.user_id,
                        principalTable: "user_profiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "flashcard_tags",
                columns: table => new
                {
                    flashcard_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    tag_id = table.Column<Guid>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_flashcard_tags", x => new { x.flashcard_id, x.tag_id });
                    table.ForeignKey(
                        name: "FK_flashcard_tags_flashcards_flashcard_id",
                        column: x => x.flashcard_id,
                        principalTable: "flashcards",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_flashcard_tags_tags_tag_id",
                        column: x => x.tag_id,
                        principalTable: "tags",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "study_records",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    user_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    flashcard_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    session_id = table.Column<Guid>(type: "TEXT", nullable: false),
                    study_mode = table.Column<string>(type: "TEXT", maxLength: 50, nullable: false),
                    quality_rating = table.Column<int>(type: "INTEGER", nullable: false),
                    response_time_ms = table.Column<int>(type: "INTEGER", nullable: true),
                    user_answer = table.Column<string>(type: "TEXT", nullable: true),
                    is_correct = table.Column<bool>(type: "INTEGER", nullable: false),
                    PreviousEasinessFactor = table.Column<float>(type: "REAL", nullable: false),
                    NewEasinessFactor = table.Column<float>(type: "REAL", nullable: false),
                    PreviousIntervalDays = table.Column<int>(type: "INTEGER", nullable: false),
                    NewIntervalDays = table.Column<int>(type: "INTEGER", nullable: false),
                    PreviousRepetitions = table.Column<int>(type: "INTEGER", nullable: false),
                    NewRepetitions = table.Column<int>(type: "INTEGER", nullable: false),
                    NextReviewDate = table.Column<DateTime>(type: "TEXT", nullable: false),
                    studied_at = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_study_records", x => x.Id);
                    table.ForeignKey(
                        name: "FK_study_records_flashcards_flashcard_id",
                        column: x => x.flashcard_id,
                        principalTable: "flashcards",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_study_records_study_sessions_session_id",
                        column: x => x.session_id,
                        principalTable: "study_sessions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_study_records_user_profiles_user_id",
                        column: x => x.user_id,
                        principalTable: "user_profiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_card_sets_UserId",
                table: "card_sets",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_daily_stats_user_id_Date",
                table: "daily_stats",
                columns: new[] { "user_id", "Date" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_error_reports_flashcard_id",
                table: "error_reports",
                column: "flashcard_id");

            migrationBuilder.CreateIndex(
                name: "IX_error_reports_resolved_by",
                table: "error_reports",
                column: "resolved_by");

            migrationBuilder.CreateIndex(
                name: "IX_error_reports_user_id",
                table: "error_reports",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_flashcard_tags_tag_id",
                table: "flashcard_tags",
                column: "tag_id");

            migrationBuilder.CreateIndex(
                name: "IX_flashcards_card_set_id",
                table: "flashcards",
                column: "card_set_id");

            migrationBuilder.CreateIndex(
                name: "IX_flashcards_user_id",
                table: "flashcards",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_SentenceAnalysisCache_Expires",
                table: "SentenceAnalysisCache",
                column: "ExpiresAt");

            migrationBuilder.CreateIndex(
                name: "IX_SentenceAnalysisCache_Hash",
                table: "SentenceAnalysisCache",
                column: "InputTextHash");

            migrationBuilder.CreateIndex(
                name: "IX_SentenceAnalysisCache_Hash_Expires",
                table: "SentenceAnalysisCache",
                columns: new[] { "InputTextHash", "ExpiresAt" });

            migrationBuilder.CreateIndex(
                name: "IX_study_records_flashcard_id",
                table: "study_records",
                column: "flashcard_id");

            migrationBuilder.CreateIndex(
                name: "IX_study_records_session_id",
                table: "study_records",
                column: "session_id");

            migrationBuilder.CreateIndex(
                name: "IX_study_records_user_id",
                table: "study_records",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_study_sessions_user_id",
                table: "study_sessions",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_tags_user_id",
                table: "tags",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_user_profiles_email",
                table: "user_profiles",
                column: "email",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_user_profiles_username",
                table: "user_profiles",
                column: "username",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_user_settings_UserId",
                table: "user_settings",
                column: "UserId",
                unique: true);
        }

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

            migrationBuilder.DropTable(
                name: "error_reports");

            migrationBuilder.DropTable(
                name: "flashcard_tags");

            migrationBuilder.DropTable(
                name: "SentenceAnalysisCache");

            migrationBuilder.DropTable(
                name: "study_records");

            migrationBuilder.DropTable(
                name: "user_settings");

            migrationBuilder.DropTable(
                name: "tags");

            migrationBuilder.DropTable(
                name: "flashcards");

            migrationBuilder.DropTable(
                name: "study_sessions");

            migrationBuilder.DropTable(
                name: "card_sets");

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