// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.30.0
// source: files.sql

package sqlc

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const createMedia = `-- name: CreateMedia :one
INSERT INTO medias (
    user_id, storage_key, original_name, mime_type, size, file_metadata
) VALUES (
    $1, $2, $3, $4, $5, $6
)
RETURNING id, user_id, storage_key, original_name, mime_type, size, file_metadata, created_at, updated_at
`

type CreateMediaParams struct {
	UserID       pgtype.UUID `json:"user_id"`
	StorageKey   string      `json:"storage_key"`
	OriginalName string      `json:"original_name"`
	MimeType     string      `json:"mime_type"`
	Size         int64       `json:"size"`
	FileMetadata []byte      `json:"file_metadata"`
}

func (q *Queries) CreateMedia(ctx context.Context, arg CreateMediaParams) (Media, error) {
	row := q.db.QueryRow(ctx, createMedia,
		arg.UserID,
		arg.StorageKey,
		arg.OriginalName,
		arg.MimeType,
		arg.Size,
		arg.FileMetadata,
	)
	var i Media
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.StorageKey,
		&i.OriginalName,
		&i.MimeType,
		&i.Size,
		&i.FileMetadata,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const deleteMedia = `-- name: DeleteMedia :exec
DELETE FROM medias
WHERE id = $1
`

func (q *Queries) DeleteMedia(ctx context.Context, id pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteMedia, id)
	return err
}

const getMediaByID = `-- name: GetMediaByID :one
SELECT id, user_id, storage_key, original_name, mime_type, size, file_metadata, created_at, updated_at FROM medias
WHERE id = $1
`

func (q *Queries) GetMediaByID(ctx context.Context, id pgtype.UUID) (Media, error) {
	row := q.db.QueryRow(ctx, getMediaByID, id)
	var i Media
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.StorageKey,
		&i.OriginalName,
		&i.MimeType,
		&i.Size,
		&i.FileMetadata,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getMediasByUserID = `-- name: GetMediasByUserID :many
SELECT id, user_id, storage_key, original_name, mime_type, size, file_metadata, created_at, updated_at FROM medias
WHERE user_id = $1
ORDER BY created_at DESC
`

func (q *Queries) GetMediasByUserID(ctx context.Context, userID pgtype.UUID) ([]Media, error) {
	rows, err := q.db.Query(ctx, getMediasByUserID, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Media{}
	for rows.Next() {
		var i Media
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.StorageKey,
			&i.OriginalName,
			&i.MimeType,
			&i.Size,
			&i.FileMetadata,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const searchMedias = `-- name: SearchMedias :many
SELECT id, user_id, storage_key, original_name, mime_type, size, file_metadata, created_at, updated_at
FROM medias
WHERE 
    ($1::uuid IS NULL OR id > $1::uuid)

    AND (
        $2::text IS NULL OR 
        original_name ILIKE '%' || $2::text || '%' OR
        storage_key ILIKE '%' || $2::text || '%'
    )

ORDER BY
    -- id
    CASE 
        WHEN $3 = 'id' AND $4 = 'asc' THEN id
    END ASC,
    CASE 
        WHEN $3 = 'id' AND $4 = 'desc' THEN id
    END DESC,

    -- created_at
    CASE 
        WHEN $3 = 'created_at' AND $4 = 'asc' THEN created_at
    END ASC,
    CASE 
        WHEN $3 = 'created_at' AND $4 = 'desc' THEN created_at
    END DESC,

    -- updated_at
    CASE 
        WHEN $3 = 'updated_at' AND $4 = 'asc' THEN updated_at
    END ASC,
    CASE 
        WHEN $3 = 'updated_at' AND $4 = 'desc' THEN updated_at
    END DESC,

    -- fallback
    id ASC

LIMIT $5
`

type SearchMediasParams struct {
	Cursor     pgtype.UUID `json:"cursor"`
	SearchText pgtype.Text `json:"search_text"`
	Sort       interface{} `json:"sort"`
	Order      interface{} `json:"order"`
	Limit      int32       `json:"limit"`
}

func (q *Queries) SearchMedias(ctx context.Context, arg SearchMediasParams) ([]Media, error) {
	rows, err := q.db.Query(ctx, searchMedias,
		arg.Cursor,
		arg.SearchText,
		arg.Sort,
		arg.Order,
		arg.Limit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Media{}
	for rows.Next() {
		var i Media
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.StorageKey,
			&i.OriginalName,
			&i.MimeType,
			&i.Size,
			&i.FileMetadata,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
