-- name: SelectStory :one
SELECT * FROM story WHERE id = $1 LIMIT 1;

-- name: SelectStories :many
SELECT * FROM story
WHERE (sqlc.arg(filter_id)::bool = false OR id = ANY(sqlc.arg(ids)::text[]))
  AND (sqlc.arg(filter_author)::bool = false OR name = sqlc.arg(author)::text)
  AND (sqlc.arg(filter_earlist_fictional_date)::bool = false OR fictional_date >= sqlc.arg(earliest_fictional_date)::timestamp)
  AND (sqlc.arg(filter_lastest_fictional_date)::bool = false OR fictional_date <= sqlc.arg(latest_fictional_date)::timestamp)
  AND (sqlc.arg(filter_category)::bool = false OR category = sqlc.arg(category)::text)
  AND (sqlc.arg(filter_open)::bool = false OR open = sqlc.arg(open)::bool)
  AND (sqlc.arg(filter_unlisted)::bool = false OR unlisted = sqlc.arg(unlisted)::bool)
ORDER BY updated_date
LIMIT sqlc.arg(limit_size)::int;

-- name: InsertStory :exec
INSERT INTO story (id, author, name, category, open, listed, sort_by_fictional_date, created_date, fictional_date, updated_date)
VALUES (
    sqlc.arg(id)::text, sqlc.arg(author)::text, sqlc.arg(name)::text, sqlc.arg(category)::text,
    sqlc.arg(open)::boolean, sqlc.arg(listed)::boolean, sqlc.arg(sort_by_fictional_date)::boolean,
    sqlc.arg(created_date)::timestamp, sqlc.arg(fictional_date)::timestamp, sqlc.arg(updated_date)::timestamp
);

-- name: UpdateStory :exec
UPDATE story
SET name=sqlc.arg(name),
    category=sqlc.arg(category),
    author=sqlc.arg(author),
    open=sqlc.arg(open),
    listed=sqlc.arg(listed),
    fictional_date=sqlc.arg(fictional_date),
    updated_date=sqlc.arg(updated_date),
    sort_by_fictional_date=sqlc.arg(sort_by_fictional_date)
WHERE id=sqlc.arg(id);

-- name: DeleteStory :exec
DELETE FROM story WHERE id=$1;