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

package db

import (
	"context"

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

const clearDefaultBilling = `-- name: ClearDefaultBilling :exec
UPDATE buyer_addresses SET is_default_billing = false, updated_at = now()
WHERE user_id = $1 AND is_default_billing
`

func (q *Queries) ClearDefaultBilling(ctx context.Context, userID int64) error {
	_, err := q.db.Exec(ctx, clearDefaultBilling, userID)
	return err
}

const clearDefaultShipping = `-- name: ClearDefaultShipping :exec
UPDATE buyer_addresses SET is_default_shipping = false, updated_at = now()
WHERE user_id = $1 AND is_default_shipping
`

func (q *Queries) ClearDefaultShipping(ctx context.Context, userID int64) error {
	_, err := q.db.Exec(ctx, clearDefaultShipping, userID)
	return err
}

const countBuyerAddresses = `-- name: CountBuyerAddresses :one
SELECT count(*) FROM buyer_addresses WHERE user_id = $1
`

func (q *Queries) CountBuyerAddresses(ctx context.Context, userID int64) (int64, error) {
	row := q.db.QueryRow(ctx, countBuyerAddresses, userID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const createBuyerAddress = `-- name: CreateBuyerAddress :one
INSERT INTO buyer_addresses (
  user_id, label, contact_name, phone, line1, line2, city, province,
  postal_code, country, latitude, longitude, is_default_shipping, is_default_billing
) VALUES (
  $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
)
RETURNING id, user_id, label, contact_name, phone, line1, line2, city, province, postal_code, country, latitude, longitude, is_default_shipping, is_default_billing, created_at, updated_at
`

type CreateBuyerAddressParams struct {
	UserID            int64         `json:"user_id"`
	Label             string        `json:"label"`
	ContactName       string        `json:"contact_name"`
	Phone             string        `json:"phone"`
	Line1             string        `json:"line1"`
	Line2             pgtype.Text   `json:"line2"`
	City              string        `json:"city"`
	Province          pgtype.Text   `json:"province"`
	PostalCode        string        `json:"postal_code"`
	Country           string        `json:"country"`
	Latitude          pgtype.Float8 `json:"latitude"`
	Longitude         pgtype.Float8 `json:"longitude"`
	IsDefaultShipping bool          `json:"is_default_shipping"`
	IsDefaultBilling  bool          `json:"is_default_billing"`
}

func (q *Queries) CreateBuyerAddress(ctx context.Context, arg CreateBuyerAddressParams) (BuyerAddress, error) {
	row := q.db.QueryRow(ctx, createBuyerAddress,
		arg.UserID,
		arg.Label,
		arg.ContactName,
		arg.Phone,
		arg.Line1,
		arg.Line2,
		arg.City,
		arg.Province,
		arg.PostalCode,
		arg.Country,
		arg.Latitude,
		arg.Longitude,
		arg.IsDefaultShipping,
		arg.IsDefaultBilling,
	)
	var i BuyerAddress
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Label,
		&i.ContactName,
		&i.Phone,
		&i.Line1,
		&i.Line2,
		&i.City,
		&i.Province,
		&i.PostalCode,
		&i.Country,
		&i.Latitude,
		&i.Longitude,
		&i.IsDefaultShipping,
		&i.IsDefaultBilling,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const deleteBuyerAddress = `-- name: DeleteBuyerAddress :execrows
DELETE FROM buyer_addresses WHERE id = $1 AND user_id = $2
`

type DeleteBuyerAddressParams struct {
	ID     int64 `json:"id"`
	UserID int64 `json:"user_id"`
}

func (q *Queries) DeleteBuyerAddress(ctx context.Context, arg DeleteBuyerAddressParams) (int64, error) {
	result, err := q.db.Exec(ctx, deleteBuyerAddress, arg.ID, arg.UserID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const getBuyerAddress = `-- name: GetBuyerAddress :one
SELECT id, user_id, label, contact_name, phone, line1, line2, city, province, postal_code, country, latitude, longitude, is_default_shipping, is_default_billing, created_at, updated_at FROM buyer_addresses
WHERE id = $1 AND user_id = $2
`

type GetBuyerAddressParams struct {
	ID     int64 `json:"id"`
	UserID int64 `json:"user_id"`
}

func (q *Queries) GetBuyerAddress(ctx context.Context, arg GetBuyerAddressParams) (BuyerAddress, error) {
	row := q.db.QueryRow(ctx, getBuyerAddress, arg.ID, arg.UserID)
	var i BuyerAddress
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Label,
		&i.ContactName,
		&i.Phone,
		&i.Line1,
		&i.Line2,
		&i.City,
		&i.Province,
		&i.PostalCode,
		&i.Country,
		&i.Latitude,
		&i.Longitude,
		&i.IsDefaultShipping,
		&i.IsDefaultBilling,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listBuyerAddresses = `-- name: ListBuyerAddresses :many
SELECT id, user_id, label, contact_name, phone, line1, line2, city, province, postal_code, country, latitude, longitude, is_default_shipping, is_default_billing, created_at, updated_at FROM buyer_addresses
WHERE user_id = $1
ORDER BY is_default_shipping DESC, created_at ASC
`

func (q *Queries) ListBuyerAddresses(ctx context.Context, userID int64) ([]BuyerAddress, error) {
	rows, err := q.db.Query(ctx, listBuyerAddresses, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BuyerAddress
	for rows.Next() {
		var i BuyerAddress
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Label,
			&i.ContactName,
			&i.Phone,
			&i.Line1,
			&i.Line2,
			&i.City,
			&i.Province,
			&i.PostalCode,
			&i.Country,
			&i.Latitude,
			&i.Longitude,
			&i.IsDefaultShipping,
			&i.IsDefaultBilling,
			&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 updateBuyerAddress = `-- name: UpdateBuyerAddress :one
UPDATE buyer_addresses SET
  label = $3,
  contact_name = $4,
  phone = $5,
  line1 = $6,
  line2 = $7,
  city = $8,
  province = $9,
  postal_code = $10,
  country = $11,
  latitude = $12,
  longitude = $13,
  is_default_shipping = $14,
  is_default_billing = $15,
  updated_at = now()
WHERE id = $1 AND user_id = $2
RETURNING id, user_id, label, contact_name, phone, line1, line2, city, province, postal_code, country, latitude, longitude, is_default_shipping, is_default_billing, created_at, updated_at
`

type UpdateBuyerAddressParams struct {
	ID                int64         `json:"id"`
	UserID            int64         `json:"user_id"`
	Label             string        `json:"label"`
	ContactName       string        `json:"contact_name"`
	Phone             string        `json:"phone"`
	Line1             string        `json:"line1"`
	Line2             pgtype.Text   `json:"line2"`
	City              string        `json:"city"`
	Province          pgtype.Text   `json:"province"`
	PostalCode        string        `json:"postal_code"`
	Country           string        `json:"country"`
	Latitude          pgtype.Float8 `json:"latitude"`
	Longitude         pgtype.Float8 `json:"longitude"`
	IsDefaultShipping bool          `json:"is_default_shipping"`
	IsDefaultBilling  bool          `json:"is_default_billing"`
}

func (q *Queries) UpdateBuyerAddress(ctx context.Context, arg UpdateBuyerAddressParams) (BuyerAddress, error) {
	row := q.db.QueryRow(ctx, updateBuyerAddress,
		arg.ID,
		arg.UserID,
		arg.Label,
		arg.ContactName,
		arg.Phone,
		arg.Line1,
		arg.Line2,
		arg.City,
		arg.Province,
		arg.PostalCode,
		arg.Country,
		arg.Latitude,
		arg.Longitude,
		arg.IsDefaultShipping,
		arg.IsDefaultBilling,
	)
	var i BuyerAddress
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Label,
		&i.ContactName,
		&i.Phone,
		&i.Line1,
		&i.Line2,
		&i.City,
		&i.Province,
		&i.PostalCode,
		&i.Country,
		&i.Latitude,
		&i.Longitude,
		&i.IsDefaultShipping,
		&i.IsDefaultBilling,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
