-- name: CreateEulaVersion :one
-- Creates a new EULA version
INSERT INTO eulaVersions (version, title, content, effectiveDate, createdBy)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, version, title, content, effectiveDate, createdAt, createdBy, isCurrent, activatedAt, activatedBy;

-- name: GetEulaVersionById :one
-- Retrieves a specific EULA version by its ID
SELECT id, version, title, content, effectiveDate, createdAt, createdBy, isCurrent, activatedAt, activatedBy
FROM eulaVersions
WHERE id = $1;

-- name: GetCurrentEulaVersion :one
-- Retrieves the current active EULA version
SELECT id, version, title, content, effectiveDate, createdAt, createdBy, isCurrent, activatedAt, activatedBy
FROM eulaVersions
WHERE isCurrent = TRUE;

-- name: ListEulaVersions :many
-- Lists all EULA versions with pagination, ordered by createdAt descending
SELECT id, version, title, content, effectiveDate, createdAt, createdBy, isCurrent, activatedAt, activatedBy
FROM eulaVersions
ORDER BY createdAt DESC
LIMIT $1 OFFSET $2;

-- name: CountEulaVersions :one
-- Counts total number of EULA versions
SELECT COUNT(*) as total
FROM eulaVersions;

-- name: UpdateEulaVersionMetadata :one
-- Updates only the metadata (title and effectiveDate) of an EULA version
UPDATE eulaVersions
SET title = $2, effectiveDate = $3
WHERE id = $1
RETURNING id, version, title, content, effectiveDate, createdAt, createdBy, isCurrent, activatedAt, activatedBy;

-- name: ClearCurrentEulaVersions :exec
-- Clears the isCurrent flag from all versions (does not clear activatedAt/By for audit trail)
UPDATE eulaVersions
SET isCurrent = FALSE
WHERE isCurrent = TRUE;

-- name: SetEulaVersionCurrent :one
-- Sets a specific version as the current active version with audit fields
UPDATE eulaVersions
SET isCurrent = TRUE, activatedAt = NOW(), activatedBy = $2
WHERE id = $1
RETURNING id, version, title, content, effectiveDate, createdAt, createdBy, isCurrent, activatedAt, activatedBy;

-- name: CreateEulaAgreement :one
-- Records a user's agreement to an EULA version
INSERT INTO eulaAgreements (cognitoSubjectId, userEmail, eulaVersionId, agreedFromIp)
VALUES ($1, $2, $3, $4)
RETURNING id, cognitoSubjectId, userEmail, eulaVersionId, agreedAt, agreedFromIp;

-- name: GetUserEulaAgreement :one
-- Checks if a user has agreed to a specific EULA version
SELECT id, cognitoSubjectId, userEmail, eulaVersionId, agreedAt, agreedFromIp
FROM eulaAgreements
WHERE cognitoSubjectId = $1 AND eulaVersionId = $2;

-- name: GetUserCurrentEulaAgreement :one
-- Checks if a user has agreed to the current EULA version
SELECT a.id, a.cognitoSubjectId, a.userEmail, a.eulaVersionId, a.agreedAt, a.agreedFromIp
FROM eulaAgreements a
JOIN eulaVersions v ON a.eulaVersionId = v.id
WHERE a.cognitoSubjectId = $1 AND v.isCurrent = TRUE;

-- name: ListEulaAgreements :many
-- Lists agreements with optional filtering by version_id or user_id
-- Pass NULL for filters to skip them
SELECT a.id, a.cognitoSubjectId, a.userEmail, a.eulaVersionId, a.agreedAt, a.agreedFromIp,
       v.version as eulaVersionString
FROM eulaAgreements a
JOIN eulaVersions v ON a.eulaVersionId = v.id
WHERE (sqlc.narg(version_id)::uuid IS NULL OR a.eulaVersionId = sqlc.narg(version_id)::uuid)
  AND (sqlc.narg(user_id)::text IS NULL OR a.cognitoSubjectId = sqlc.narg(user_id)::text)
ORDER BY a.agreedAt DESC
LIMIT $1 OFFSET $2;

-- name: CountEulaAgreements :one
-- Counts agreements with optional filtering (matches ListEulaAgreements filters)
SELECT COUNT(*) as total
FROM eulaAgreements a
WHERE (sqlc.narg(version_id)::uuid IS NULL OR a.eulaVersionId = sqlc.narg(version_id)::uuid)
  AND (sqlc.narg(user_id)::text IS NULL OR a.cognitoSubjectId = sqlc.narg(user_id)::text);

-- name: ListEulaAgreementsByUser :many
-- Gets all EULA versions a specific user has agreed to (user's complete history)
SELECT a.id, a.cognitoSubjectId, a.userEmail, a.eulaVersionId, a.agreedAt, a.agreedFromIp,
       v.version as eulaVersionString, v.title as eulaVersionTitle
FROM eulaAgreements a
JOIN eulaVersions v ON a.eulaVersionId = v.id
WHERE a.cognitoSubjectId = $1
ORDER BY a.agreedAt DESC;

-- name: CountEulaAgreementsByVersion :one
-- Counts how many users have agreed to a specific EULA version
SELECT COUNT(*) as count
FROM eulaAgreements
WHERE eulaVersionId = $1;

-- name: ListAgreedUsersForVersion :many
-- Lists all users who have agreed to a specific EULA version
SELECT id, cognitoSubjectId, userEmail, eulaVersionId, agreedAt, agreedFromIp
FROM eulaAgreements
WHERE eulaVersionId = $1
ORDER BY agreedAt DESC
LIMIT $2 OFFSET $3;
