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

package repository

import (
	"context"

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

const createContact = `-- name: CreateContact :one
INSERT INTO contacts (id, owner_id, first_name, last_name, email, phone, company, notes)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id, owner_id, first_name, last_name, email, phone, company, notes, created_at, updated_at, deleted_at
`

type CreateContactParams struct {
	ID        pgtype.UUID `json:"id"`
	OwnerID   pgtype.UUID `json:"owner_id"`
	FirstName pgtype.Text `json:"first_name"`
	LastName  pgtype.Text `json:"last_name"`
	Email     pgtype.Text `json:"email"`
	Phone     pgtype.Text `json:"phone"`
	Company   pgtype.Text `json:"company"`
	Notes     pgtype.Text `json:"notes"`
}

func (q *Queries) CreateContact(ctx context.Context, arg CreateContactParams) (Contact, error) {
	row := q.db.QueryRow(ctx, createContact,
		arg.ID,
		arg.OwnerID,
		arg.FirstName,
		arg.LastName,
		arg.Email,
		arg.Phone,
		arg.Company,
		arg.Notes,
	)
	var i Contact
	err := row.Scan(
		&i.ID,
		&i.OwnerID,
		&i.FirstName,
		&i.LastName,
		&i.Email,
		&i.Phone,
		&i.Company,
		&i.Notes,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const getContactByID = `-- name: GetContactByID :one
SELECT id, owner_id, first_name, last_name, email, phone, company, notes, created_at, updated_at, deleted_at FROM contacts
WHERE id = $1 AND owner_id = $2 AND deleted_at IS NULL
`

type GetContactByIDParams struct {
	ID      pgtype.UUID `json:"id"`
	OwnerID pgtype.UUID `json:"owner_id"`
}

func (q *Queries) GetContactByID(ctx context.Context, arg GetContactByIDParams) (Contact, error) {
	row := q.db.QueryRow(ctx, getContactByID, arg.ID, arg.OwnerID)
	var i Contact
	err := row.Scan(
		&i.ID,
		&i.OwnerID,
		&i.FirstName,
		&i.LastName,
		&i.Email,
		&i.Phone,
		&i.Company,
		&i.Notes,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}

const listContacts = `-- name: ListContacts :many
SELECT id, owner_id, first_name, last_name, email, phone, company, notes, created_at, updated_at, deleted_at FROM contacts
WHERE owner_id = $1
  AND deleted_at IS NULL
  AND (
    $2::TEXT IS NULL
    OR first_name ILIKE '%' || $2::TEXT || '%'
    OR last_name ILIKE '%' || $2::TEXT || '%'
    OR email ILIKE '%' || $2::TEXT || '%'
    OR company ILIKE '%' || $2::TEXT || '%'
  )
  AND (
    $3::TIMESTAMPTZ IS NULL
    OR (created_at, id) > ($3::TIMESTAMPTZ, $4::UUID)
  )
ORDER BY created_at ASC, id ASC
LIMIT $5
`

type ListContactsParams struct {
	OwnerID    pgtype.UUID        `json:"owner_id"`
	Search     pgtype.Text        `json:"search"`
	CursorTime pgtype.Timestamptz `json:"cursor_time"`
	CursorID   pgtype.UUID        `json:"cursor_id"`
	Lim        int32              `json:"lim"`
}

func (q *Queries) ListContacts(ctx context.Context, arg ListContactsParams) ([]Contact, error) {
	rows, err := q.db.Query(ctx, listContacts,
		arg.OwnerID,
		arg.Search,
		arg.CursorTime,
		arg.CursorID,
		arg.Lim,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Contact{}
	for rows.Next() {
		var i Contact
		if err := rows.Scan(
			&i.ID,
			&i.OwnerID,
			&i.FirstName,
			&i.LastName,
			&i.Email,
			&i.Phone,
			&i.Company,
			&i.Notes,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.DeletedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const softDeleteContact = `-- name: SoftDeleteContact :exec
UPDATE contacts SET deleted_at = now(), updated_at = now()
WHERE id = $1 AND owner_id = $2 AND deleted_at IS NULL
`

type SoftDeleteContactParams struct {
	ID      pgtype.UUID `json:"id"`
	OwnerID pgtype.UUID `json:"owner_id"`
}

func (q *Queries) SoftDeleteContact(ctx context.Context, arg SoftDeleteContactParams) error {
	_, err := q.db.Exec(ctx, softDeleteContact, arg.ID, arg.OwnerID)
	return err
}

const softDeleteContactsByOwner = `-- name: SoftDeleteContactsByOwner :exec
UPDATE contacts SET deleted_at = now(), updated_at = now()
WHERE owner_id = $1 AND deleted_at IS NULL
`

func (q *Queries) SoftDeleteContactsByOwner(ctx context.Context, ownerID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, softDeleteContactsByOwner, ownerID)
	return err
}

const updateContact = `-- name: UpdateContact :one
UPDATE contacts
SET first_name = COALESCE($1, first_name),
    last_name = COALESCE($2, last_name),
    email = COALESCE($3, email),
    phone = COALESCE($4, phone),
    company = COALESCE($5, company),
    notes = COALESCE($6, notes),
    updated_at = now()
WHERE id = $7 AND owner_id = $8 AND deleted_at IS NULL
RETURNING id, owner_id, first_name, last_name, email, phone, company, notes, created_at, updated_at, deleted_at
`

type UpdateContactParams struct {
	FirstName pgtype.Text `json:"first_name"`
	LastName  pgtype.Text `json:"last_name"`
	Email     pgtype.Text `json:"email"`
	Phone     pgtype.Text `json:"phone"`
	Company   pgtype.Text `json:"company"`
	Notes     pgtype.Text `json:"notes"`
	ID        pgtype.UUID `json:"id"`
	OwnerID   pgtype.UUID `json:"owner_id"`
}

func (q *Queries) UpdateContact(ctx context.Context, arg UpdateContactParams) (Contact, error) {
	row := q.db.QueryRow(ctx, updateContact,
		arg.FirstName,
		arg.LastName,
		arg.Email,
		arg.Phone,
		arg.Company,
		arg.Notes,
		arg.ID,
		arg.OwnerID,
	)
	var i Contact
	err := row.Scan(
		&i.ID,
		&i.OwnerID,
		&i.FirstName,
		&i.LastName,
		&i.Email,
		&i.Phone,
		&i.Company,
		&i.Notes,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.DeletedAt,
	)
	return i, err
}
