-- ============================================================
-- Prompt Rule Groups
-- ============================================================

-- name: ListPromptRuleGroups :many
SELECT id, tenant_id, name, sort_order, created_at, updated_at
FROM prompt_rule_groups
WHERE tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL
ORDER BY sort_order, created_at;

-- name: CreatePromptRuleGroup :one
INSERT INTO prompt_rule_groups (tenant_id, name, sort_order)
VALUES (sqlc.arg(tenant_id), sqlc.arg(name), sqlc.arg(sort_order))
RETURNING id, created_at;

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

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

-- ============================================================
-- Prompt Rules
-- ============================================================

-- name: ListPromptRules :many
SELECT pr.id, pr.tenant_id, pr.group_id, pr.name, pr.prompt_content,
       pr.scene, pr.default_tone, pr.default_word_count, pr.status,
       pr.created_at, pr.updated_at,
       COUNT(a.id)::INT AS article_count
FROM prompt_rules pr
LEFT JOIN articles a ON a.prompt_rule_id = pr.id AND a.deleted_at IS NULL
WHERE pr.tenant_id = sqlc.arg(tenant_id)
  AND pr.deleted_at IS NULL
  AND (sqlc.narg(group_id)::bigint IS NULL OR pr.group_id = sqlc.narg(group_id))
  AND (sqlc.narg(status)::text IS NULL OR pr.status = sqlc.narg(status))
  AND (sqlc.narg(keyword)::text IS NULL OR pr.name ILIKE '%' || sqlc.narg(keyword) || '%')
GROUP BY pr.id
ORDER BY pr.created_at DESC
LIMIT sqlc.arg(page_size) OFFSET sqlc.arg(page_offset);

-- name: CountPromptRules :one
SELECT COUNT(*)
FROM prompt_rules
WHERE tenant_id = sqlc.arg(tenant_id)
  AND deleted_at IS NULL
  AND (sqlc.narg(group_id)::bigint IS NULL OR group_id = sqlc.narg(group_id))
  AND (sqlc.narg(status)::text IS NULL OR status = sqlc.narg(status))
  AND (sqlc.narg(keyword)::text IS NULL OR name ILIKE '%' || sqlc.narg(keyword) || '%');

-- name: ListPromptRulesUngrouped :many
SELECT pr.id, pr.tenant_id, pr.group_id, pr.name, pr.prompt_content,
       pr.scene, pr.default_tone, pr.default_word_count, pr.status,
       pr.created_at, pr.updated_at,
       COUNT(a.id)::INT AS article_count
FROM prompt_rules pr
LEFT JOIN articles a ON a.prompt_rule_id = pr.id AND a.deleted_at IS NULL
WHERE pr.tenant_id = sqlc.arg(tenant_id)
  AND pr.deleted_at IS NULL
  AND pr.group_id IS NULL
GROUP BY pr.id
ORDER BY pr.created_at DESC
LIMIT sqlc.arg(page_size) OFFSET sqlc.arg(page_offset);

-- name: CountPromptRulesUngrouped :one
SELECT COUNT(*)
FROM prompt_rules
WHERE tenant_id = sqlc.arg(tenant_id)
  AND deleted_at IS NULL
  AND group_id IS NULL;

-- name: GetPromptRuleByID :one
SELECT id, tenant_id, group_id, name, prompt_content,
       scene, default_tone, default_word_count, status,
       created_at, updated_at
FROM prompt_rules
WHERE id = sqlc.arg(id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;

-- name: CreatePromptRule :one
INSERT INTO prompt_rules (tenant_id, group_id, name, prompt_content, scene, default_tone, default_word_count)
VALUES (sqlc.arg(tenant_id), sqlc.narg(group_id), sqlc.arg(name), sqlc.arg(prompt_content),
        sqlc.narg(scene), sqlc.narg(default_tone), sqlc.narg(default_word_count))
RETURNING id, created_at;

-- name: UpdatePromptRule :exec
UPDATE prompt_rules
SET name = sqlc.arg(name), group_id = sqlc.narg(group_id),
    prompt_content = sqlc.arg(prompt_content),
    scene = sqlc.narg(scene), default_tone = sqlc.narg(default_tone),
    default_word_count = sqlc.narg(default_word_count), updated_at = NOW()
WHERE id = sqlc.arg(id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;

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

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

-- name: ListAllPromptRulesSimple :many
SELECT id, name, status
FROM prompt_rules
WHERE tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL AND status = 'enabled'
ORDER BY name;
