using System;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Tiku.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class AddVideoPlaybackProgress : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "video_playback_progress",
                columns: table => new
                {
                    id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
                    user_id = table.Column<Guid>(type: "uuid", nullable: false),
                    video_id = table.Column<Guid>(type: "uuid", nullable: false),
                    question_id = table.Column<Guid>(type: "uuid", nullable: true),
                    position_seconds = table.Column<int>(type: "integer", nullable: false),
                    duration_seconds = table.Column<int>(type: "integer", nullable: true),
                    watched_seconds = table.Column<int>(type: "integer", nullable: false),
                    is_completed = table.Column<bool>(type: "boolean", nullable: false),
                    completed_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
                    last_played_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
                    play_count = table.Column<int>(type: "integer", nullable: false),
                    metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
                    tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
                    created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
                    updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_video_playback_progress", x => x.id);
                    table.UniqueConstraint("ak_video_playback_progress_tenant_id_id", x => new { x.tenant_id, x.id });
                    table.CheckConstraint("ck_video_playback_progress_duration", "duration_seconds is null or duration_seconds >= 0");
                    table.CheckConstraint("ck_video_playback_progress_play_count", "play_count >= 0");
                    table.CheckConstraint("ck_video_playback_progress_position", "position_seconds >= 0");
                    table.CheckConstraint("ck_video_playback_progress_watched", "watched_seconds >= 0");
                    table.ForeignKey(
                        name: "fk_video_playback_progress_questions_tenant_id_question_id",
                        columns: x => new { x.tenant_id, x.question_id },
                        principalTable: "questions",
                        principalColumns: new[] { "tenant_id", "id" },
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "fk_video_playback_progress_tenants_tenant_id",
                        column: x => x.tenant_id,
                        principalTable: "tenants",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "fk_video_playback_progress_users_user_id",
                        column: x => x.user_id,
                        principalTable: "users",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "fk_video_playback_progress_video_explanations_tenant_id_video_~",
                        columns: x => new { x.tenant_id, x.video_id },
                        principalTable: "video_explanations",
                        principalColumns: new[] { "tenant_id", "id" },
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "ix_video_playback_progress_tenant_id_question_id",
                table: "video_playback_progress",
                columns: new[] { "tenant_id", "question_id" });

            migrationBuilder.CreateIndex(
                name: "ix_video_playback_progress_tenant_id_user_id_last_played_at",
                table: "video_playback_progress",
                columns: new[] { "tenant_id", "user_id", "last_played_at" });

            migrationBuilder.CreateIndex(
                name: "ix_video_playback_progress_tenant_id_user_id_video_id",
                table: "video_playback_progress",
                columns: new[] { "tenant_id", "user_id", "video_id" },
                unique: true,
                filter: "question_id is null");

            migrationBuilder.CreateIndex(
                name: "ix_video_playback_progress_tenant_id_user_id_video_id_question~",
                table: "video_playback_progress",
                columns: new[] { "tenant_id", "user_id", "video_id", "question_id" },
                unique: true,
                filter: "question_id is not null");

            migrationBuilder.CreateIndex(
                name: "ix_video_playback_progress_tenant_id_video_id",
                table: "video_playback_progress",
                columns: new[] { "tenant_id", "video_id" });

            migrationBuilder.CreateIndex(
                name: "ix_video_playback_progress_user_id",
                table: "video_playback_progress",
                column: "user_id");
        }

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