// 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 countMedias = `-- name: CountMedias :one
SELECT count(*) 
FROM medias
WHERE 
    ($1::uuid[] IS NULL OR user_id = ANY($1::uuid[]))
    AND ($2::text IS NULL OR mime_type ILIKE $2::text || '%')
    AND ($3::bigint IS NULL OR size >= $3::bigint)
    AND ($4::bigint IS NULL OR size <= $4::bigint)
    AND (
        $5::text IS NULL OR 
        id::text ILIKE '%' || $5::text || '%' OR
        original_name ILIKE '%' || $5::text || '%' OR
        storage_key ILIKE '%' || $5::text || '%'
    )
`

type CountMediasParams struct {
	UserIds    []pgtype.UUID `json:"user_ids"`
	MimeType   pgtype.Text   `json:"mime_type"`
	MinSize    pgtype.Int8   `json:"min_size"`
	MaxSize    pgtype.Int8   `json:"max_size"`
	SearchText pgtype.Text   `json:"search_text"`
}

func (q *Queries) CountMedias(ctx context.Context, arg CountMediasParams) (int64, error) {
	row := q.db.QueryRow(ctx, countMedias,
		arg.UserIds,
		arg.MimeType,
		arg.MinSize,
		arg.MaxSize,
		arg.SearchText,
	)
	var count int64
	err := row.Scan(&count)
	return count, err
}

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 deleteMedias = `-- name: DeleteMedias :exec
DELETE FROM medias
WHERE id = ANY($1::uuid[])
`

func (q *Queries) DeleteMedias(ctx context.Context, dollar_1 []pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteMedias, dollar_1)
	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 getMediaByIDs = `-- name: GetMediaByIDs :many
SELECT id, user_id, storage_key, original_name, mime_type, size, file_metadata, created_at, updated_at FROM medias
WHERE id = ANY($1::uuid[])
`

func (q *Queries) GetMediaByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]Media, error) {
	rows, err := q.db.Query(ctx, getMediaByIDs, dollar_1)
	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 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 user_id = ANY($1::uuid[]))
    AND ($2::text IS NULL OR mime_type ILIKE $2::text || '%')
    AND ($3::bigint IS NULL OR size >= $3::bigint)
    AND ($4::bigint IS NULL OR size <= $4::bigint)
    AND (
        $5::text IS NULL OR 
        id::text ILIKE '%' || $5::text || '%' OR
        original_name ILIKE '%' || $5::text || '%' OR
        storage_key ILIKE '%' || $5::text || '%'
    )
ORDER BY
    CASE WHEN $6 = 'id' AND $7 = 'asc' THEN id END ASC,
    CASE WHEN $6 = 'id' AND $7 = 'desc' THEN id END DESC,

    CASE WHEN $6 = 'created_at' AND $7 = 'asc' THEN created_at END ASC,
    CASE WHEN $6 = 'created_at' AND $7 = 'desc' THEN created_at END DESC,

    CASE WHEN $6 = 'updated_at' AND $7 = 'asc' THEN updated_at END ASC,
    CASE WHEN $6 = 'updated_at' AND $7 = 'desc' THEN updated_at END DESC,

    CASE WHEN $6 = 'size' AND $7 = 'asc' THEN size END ASC,
    CASE WHEN $6 = 'size' AND $7 = 'desc' THEN size END DESC,

    CASE WHEN $6 = 'original_name' AND $7 = 'asc' THEN original_name END ASC,
    CASE WHEN $6 = 'original_name' AND $7 = 'desc' THEN original_name END DESC,
 
    CASE WHEN $6 = 'storage_key' AND $7 = 'asc' THEN storage_key END ASC,
    CASE WHEN $6 = 'storage_key' AND $7 = 'desc' THEN storage_key END DESC,

    CASE WHEN $6 = 'mime_type' AND $7 = 'asc' THEN mime_type END ASC,
    CASE WHEN $6 = 'mime_type' AND $7 = 'desc' THEN mime_type END DESC,
    
    id ASC
LIMIT $9
OFFSET $8
`

type SearchMediasParams struct {
	UserIds    []pgtype.UUID `json:"user_ids"`
	MimeType   pgtype.Text   `json:"mime_type"`
	MinSize    pgtype.Int8   `json:"min_size"`
	MaxSize    pgtype.Int8   `json:"max_size"`
	SearchText pgtype.Text   `json:"search_text"`
	Sort       interface{}   `json:"sort"`
	Order      interface{}   `json:"order"`
	Offset     int32         `json:"offset"`
	Limit      int32         `json:"limit"`
}

func (q *Queries) SearchMedias(ctx context.Context, arg SearchMediasParams) ([]Media, error) {
	rows, err := q.db.Query(ctx, searchMedias,
		arg.UserIds,
		arg.MimeType,
		arg.MinSize,
		arg.MaxSize,
		arg.SearchText,
		arg.Sort,
		arg.Order,
		arg.Offset,
		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
}
