-- name: ListBrands :many
SELECT id, tenant_id, name, description, status, created_at, updated_at
FROM brands
WHERE tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL
ORDER BY created_at DESC;

-- name: GetBrandByID :one
SELECT id, tenant_id, name, description, status, created_at, updated_at
FROM brands
WHERE id = sqlc.arg(id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;

-- name: CreateBrand :one
INSERT INTO brands (tenant_id, name, description, status)
VALUES (sqlc.arg(tenant_id), sqlc.arg(name), sqlc.arg(description), 'active')
RETURNING id, created_at;

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

-- name: SoftDeleteBrand :exec
UPDATE brands 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: ListKeywords :many
SELECT id, tenant_id, brand_id, name, status, created_at, updated_at
FROM brand_keywords
WHERE brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL
ORDER BY created_at DESC;

-- name: CreateKeyword :one
INSERT INTO brand_keywords (tenant_id, brand_id, name, status)
VALUES (sqlc.arg(tenant_id), sqlc.arg(brand_id), sqlc.arg(name), 'active')
RETURNING id, created_at;

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

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

-- name: SoftDeleteKeywordsByBrand :exec
UPDATE brand_keywords SET deleted_at = NOW(), updated_at = NOW()
WHERE brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;

-- name: ListQuestions :many
SELECT q.id, q.tenant_id, q.brand_id, q.keyword_id, q.question_text, q.status, q.created_at
FROM brand_questions q
WHERE q.brand_id = sqlc.arg(brand_id) AND q.tenant_id = sqlc.arg(tenant_id) AND q.deleted_at IS NULL
  AND (sqlc.narg(keyword_id)::bigint IS NULL OR q.keyword_id = sqlc.narg(keyword_id))
ORDER BY q.created_at DESC;

-- name: CreateQuestion :one
INSERT INTO brand_questions (tenant_id, brand_id, keyword_id, question_text, status)
VALUES (sqlc.arg(tenant_id), sqlc.arg(brand_id), sqlc.arg(keyword_id), sqlc.arg(question_text), 'active')
RETURNING id;

-- name: UpdateQuestion :exec
UPDATE brand_questions SET question_text = sqlc.arg(question_text), updated_at = NOW()
WHERE id = sqlc.arg(id) AND brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;

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

-- name: SoftDeleteQuestionsByBrand :exec
UPDATE brand_questions SET deleted_at = NOW(), updated_at = NOW()
WHERE brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;

-- name: ListCompetitors :many
SELECT id, tenant_id, brand_id, name, website, description, product_lines_json, created_at, updated_at
FROM competitors
WHERE brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL
ORDER BY created_at DESC;

-- name: CreateCompetitor :one
INSERT INTO competitors (tenant_id, brand_id, name, website, description, product_lines_json)
VALUES (sqlc.arg(tenant_id), sqlc.arg(brand_id), sqlc.arg(name), sqlc.arg(website), sqlc.arg(description), sqlc.arg(product_lines_json))
RETURNING id, created_at;

-- name: UpdateCompetitor :exec
UPDATE competitors SET name = sqlc.arg(name), website = sqlc.arg(website),
       description = sqlc.arg(description), product_lines_json = sqlc.arg(product_lines_json), updated_at = NOW()
WHERE id = sqlc.arg(id) AND brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;

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

-- name: SoftDeleteCompetitorsByBrand :exec
UPDATE competitors SET deleted_at = NOW(), updated_at = NOW()
WHERE brand_id = sqlc.arg(brand_id) AND tenant_id = sqlc.arg(tenant_id) AND deleted_at IS NULL;
