-- name: CreateModuleVideo :one
INSERT INTO module_videos (
    module_id,
    title,
    description,
    video_url,
    duration,
    resolution,

    is_published,
    publish_date,
    visibility,

    instructor_id,
    thumbnail,
    is_active
)
VALUES (
    $1,  -- module_id
    $2,  -- title
    $3,  -- description
    $4,  -- video_url
    $5,  -- duration
    $6,  -- resolution

    $7,  -- is_published
    $8,  -- publish_date
    $9,  -- visibility

    $10, -- instructor_id
    $11, -- thumbnail
    $12  -- is_active
)
RETURNING
    id,
    module_id,
    title,
    description,
    video_url,
    duration,
    resolution,
    is_published,
    publish_date,
    visibility,
    instructor_id,
    thumbnail,
    is_active;

-- name: GetModuleVideoByID :one
SELECT
    id,
    module_id,
    title,
    description,
    video_url,
    duration,
    resolution,
    is_published,
    publish_date,
    visibility,
    instructor_id,
    thumbnail,
    is_active
FROM module_videos
WHERE id = $1;

-- name: ListPublishedVideosByModule :many
SELECT
    id,
    module_id,
    title,
    description,
    video_url,
    duration,
    resolution,
    publish_date,
    visibility,
    instructor_id,
    thumbnail
FROM module_videos
WHERE module_id = $1
  AND is_active = TRUE
  AND is_published = TRUE
ORDER BY publish_date ASC, id ASC;

-- name: ListAllVideosByModule :many
SELECT
    id,
    module_id,
    title,
    description,
    video_url,
    duration,
    resolution,
    is_published,
    publish_date,
    visibility,
    instructor_id,
    thumbnail,
    is_active
FROM module_videos
WHERE module_id = $1
ORDER BY id ASC;

-- name: UpdateModuleVideo :one
UPDATE module_videos
SET
    title = $2,
    description = $3,
    video_url = $4,
    duration = $5,
    resolution = $6,

    is_published = $7,
    publish_date = $8,
    visibility = $9,

    instructor_id = $10,
    thumbnail = $11,
    is_active = $12
WHERE id = $1
RETURNING
    id,
    module_id,
    title,
    description,
    video_url,
    duration,
    resolution,
    is_published,
    publish_date,
    visibility,
    instructor_id,
    thumbnail,
    is_active;

-- name: DeactivateModuleVideo :exec
UPDATE module_videos
SET is_active = FALSE
WHERE id = $1;

