-- name: ListArticles :many
SELECT a.id, a.tenant_id, a.source_type, a.template_id,
       a.generate_status, a.publish_status, a.created_at, a.updated_at,
       av.title, av.word_count, av.source_label,
       t.template_name
FROM articles a
LEFT JOIN article_versions av ON av.id = a.current_version_id
LEFT JOIN article_templates t ON t.id = a.template_id
WHERE a.tenant_id = sqlc.arg(tenant_id)
  AND a.deleted_at IS NULL
  AND (sqlc.narg(generate_status)::text IS NULL OR a.generate_status = sqlc.narg(generate_status))
  AND (sqlc.narg(publish_status)::text IS NULL OR a.publish_status = sqlc.narg(publish_status))
  AND (sqlc.narg(source_type)::text IS NULL OR a.source_type = sqlc.narg(source_type))
  AND (sqlc.narg(template_id)::bigint IS NULL OR a.template_id = sqlc.narg(template_id))
  AND (sqlc.narg(keyword)::text IS NULL OR av.title ILIKE '%' || sqlc.narg(keyword) || '%')
ORDER BY a.created_at DESC
LIMIT sqlc.arg(page_size) OFFSET sqlc.arg(page_offset);

-- name: CountArticles :one
SELECT COUNT(*)
FROM articles a
LEFT JOIN article_versions av ON av.id = a.current_version_id
WHERE a.tenant_id = sqlc.arg(tenant_id)
  AND a.deleted_at IS NULL
  AND (sqlc.narg(generate_status)::text IS NULL OR a.generate_status = sqlc.narg(generate_status))
  AND (sqlc.narg(publish_status)::text IS NULL OR a.publish_status = sqlc.narg(publish_status))
  AND (sqlc.narg(source_type)::text IS NULL OR a.source_type = sqlc.narg(source_type))
  AND (sqlc.narg(template_id)::bigint IS NULL OR a.template_id = sqlc.narg(template_id))
  AND (sqlc.narg(keyword)::text IS NULL OR av.title ILIKE '%' || sqlc.narg(keyword) || '%');

-- name: GetArticleByID :one
SELECT a.id, a.tenant_id, a.source_type, a.template_id, a.current_version_id,
       a.generate_status, a.publish_status, a.created_at, a.updated_at,
       av.title, av.html_content, av.markdown_content, av.word_count, av.source_label, av.version_no,
       t.template_name
FROM articles a
LEFT JOIN article_versions av ON av.id = a.current_version_id
LEFT JOIN article_templates t ON t.id = a.template_id
WHERE a.id = sqlc.arg(id) AND a.tenant_id = sqlc.arg(tenant_id) AND a.deleted_at IS NULL;

-- name: GetArticleVersions :many
SELECT id, article_id, version_no, title, word_count, source_label, created_at
FROM article_versions
WHERE article_id = sqlc.arg(article_id)
ORDER BY version_no DESC;

-- name: CreateArticle :one
INSERT INTO articles (tenant_id, source_type, template_id, kol_prompt_id, generate_status, publish_status)
VALUES (sqlc.arg(tenant_id), sqlc.arg(source_type), sqlc.arg(template_id), sqlc.arg(kol_prompt_id), 'pending', 'unpublished')
RETURNING id;

-- name: CreateArticleVersion :one
INSERT INTO article_versions (article_id, version_no, title, html_content, markdown_content, word_count, source_label)
VALUES (sqlc.arg(article_id), sqlc.arg(version_no), sqlc.arg(title), sqlc.arg(html_content), sqlc.arg(markdown_content), sqlc.arg(word_count), sqlc.arg(source_label))
RETURNING id;

-- name: UpdateArticleCurrentVersion :exec
UPDATE articles SET current_version_id = sqlc.arg(version_id), updated_at = NOW()
WHERE id = sqlc.arg(id) AND tenant_id = sqlc.arg(tenant_id);

-- name: UpdateArticleGenerateStatus :exec
UPDATE articles SET generate_status = sqlc.arg(status), updated_at = NOW()
WHERE id = sqlc.arg(id) AND tenant_id = sqlc.arg(tenant_id);

-- name: SoftDeleteArticle :exec
UPDATE articles SET deleted_at = NOW(), updated_at = NOW()
WHERE id = sqlc.arg(id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;
