// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.27.0
// source: url.sql

package repo

import (
	"context"
	"time"
)

const createURL = `-- name: CreateURL :exec
INSERT INTO urls (
    original_url,
    short_code,
    is_custom,
    expired_at,
    user_id
) VALUES (
    $1, $2, $3, $4, $5
)
`

type CreateURLParams struct {
	OriginalUrl string    `json:"original_url"`
	ShortCode   string    `json:"short_code"`
	IsCustom    bool      `json:"is_custom"`
	ExpiredAt   time.Time `json:"expired_at"`
	UserID      int32     `json:"user_id"`
}

func (q *Queries) CreateURL(ctx context.Context, arg CreateURLParams) error {
	_, err := q.db.ExecContext(ctx, createURL,
		arg.OriginalUrl,
		arg.ShortCode,
		arg.IsCustom,
		arg.ExpiredAt,
		arg.UserID,
	)
	return err
}

const deleteURLByShortCode = `-- name: DeleteURLByShortCode :exec
DELETE FROM urls
WHERE short_code = $1
`

func (q *Queries) DeleteURLByShortCode(ctx context.Context, shortCode string) error {
	_, err := q.db.ExecContext(ctx, deleteURLByShortCode, shortCode)
	return err
}

const getURLsByUserID = `-- name: GetURLsByUserID :many
SELECT id, original_url, short_code, views, is_custom, expired_at, COUNT(*) OVER() AS total
FROM urls r
WHERE r.user_id = $1
ORDER BY created_at DESC
LIMIT $2 OFFSET $3
`

type GetURLsByUserIDParams struct {
	UserID int32 `json:"user_id"`
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

type GetURLsByUserIDRow struct {
	ID          int64     `json:"id"`
	OriginalUrl string    `json:"original_url"`
	ShortCode   string    `json:"short_code"`
	Views       int32     `json:"views"`
	IsCustom    bool      `json:"is_custom"`
	ExpiredAt   time.Time `json:"expired_at"`
	Total       int64     `json:"total"`
}

func (q *Queries) GetURLsByUserID(ctx context.Context, arg GetURLsByUserIDParams) ([]GetURLsByUserIDRow, error) {
	rows, err := q.db.QueryContext(ctx, getURLsByUserID, arg.UserID, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetURLsByUserIDRow
	for rows.Next() {
		var i GetURLsByUserIDRow
		if err := rows.Scan(
			&i.ID,
			&i.OriginalUrl,
			&i.ShortCode,
			&i.Views,
			&i.IsCustom,
			&i.ExpiredAt,
			&i.Total,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getUrlByShortCode = `-- name: GetUrlByShortCode :one
SELECT original_url, short_code, views, is_custom FROM urls 
WHERE short_code = $1
AND expired_at > CURRENT_TIMESTAMP
`

type GetUrlByShortCodeRow struct {
	OriginalUrl string `json:"original_url"`
	ShortCode   string `json:"short_code"`
	Views       int32  `json:"views"`
	IsCustom    bool   `json:"is_custom"`
}

func (q *Queries) GetUrlByShortCode(ctx context.Context, shortCode string) (GetUrlByShortCodeRow, error) {
	row := q.db.QueryRowContext(ctx, getUrlByShortCode, shortCode)
	var i GetUrlByShortCodeRow
	err := row.Scan(
		&i.OriginalUrl,
		&i.ShortCode,
		&i.Views,
		&i.IsCustom,
	)
	return i, err
}

const isShortCodeAvailable = `-- name: IsShortCodeAvailable :one
SELECT NOT EXISTS(
    SELECT 1 FROM urls
    WHERE short_code = $1
) AS is_available
`

func (q *Queries) IsShortCodeAvailable(ctx context.Context, shortCode string) (bool, error) {
	row := q.db.QueryRowContext(ctx, isShortCodeAvailable, shortCode)
	var is_available bool
	err := row.Scan(&is_available)
	return is_available, err
}

const updateURLExpiredByShortCode = `-- name: UpdateURLExpiredByShortCode :exec
UPDATE urls
SET expired_at = $1
WHERE short_code = $2
`

type UpdateURLExpiredByShortCodeParams struct {
	ExpiredAt time.Time `json:"expired_at"`
	ShortCode string    `json:"short_code"`
}

func (q *Queries) UpdateURLExpiredByShortCode(ctx context.Context, arg UpdateURLExpiredByShortCodeParams) error {
	_, err := q.db.ExecContext(ctx, updateURLExpiredByShortCode, arg.ExpiredAt, arg.ShortCode)
	return err
}

const updateViewsByShortCode = `-- name: UpdateViewsByShortCode :exec
UPDATE urls
SET views = views + $1
WHERE short_code = $2
`

type UpdateViewsByShortCodeParams struct {
	Views     int32  `json:"views"`
	ShortCode string `json:"short_code"`
}

func (q *Queries) UpdateViewsByShortCode(ctx context.Context, arg UpdateViewsByShortCodeParams) error {
	_, err := q.db.ExecContext(ctx, updateViewsByShortCode, arg.Views, arg.ShortCode)
	return err
}
