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

package querier

import (
	"context"
)

const createOrg = `-- name: CreateOrg :one
INSERT INTO anclax.orgs (name) VALUES ($1) RETURNING id, name, tz, created_at, updated_at
`

func (q *Queries) CreateOrg(ctx context.Context, name string) (*AnclaxOrg, error) {
	row := q.db.QueryRow(ctx, createOrg, name)
	var i AnclaxOrg
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Tz,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return &i, err
}

const getOrg = `-- name: GetOrg :one
SELECT id, name, tz, created_at, updated_at FROM anclax.orgs WHERE id = $1
`

func (q *Queries) GetOrg(ctx context.Context, id int32) (*AnclaxOrg, error) {
	row := q.db.QueryRow(ctx, getOrg, id)
	var i AnclaxOrg
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Tz,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return &i, err
}

const getOrgByName = `-- name: GetOrgByName :one
SELECT id, name, tz, created_at, updated_at FROM anclax.orgs WHERE name = $1
`

func (q *Queries) GetOrgByName(ctx context.Context, name string) (*AnclaxOrg, error) {
	row := q.db.QueryRow(ctx, getOrgByName, name)
	var i AnclaxOrg
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Tz,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return &i, err
}

const insertOrgOwner = `-- name: InsertOrgOwner :one
INSERT INTO anclax.org_owners (org_id, user_id) VALUES ($1, $2) RETURNING org_id, user_id, created_at
`

type InsertOrgOwnerParams struct {
	OrgID  int32
	UserID int32
}

func (q *Queries) InsertOrgOwner(ctx context.Context, arg InsertOrgOwnerParams) (*AnclaxOrgOwner, error) {
	row := q.db.QueryRow(ctx, insertOrgOwner, arg.OrgID, arg.UserID)
	var i AnclaxOrgOwner
	err := row.Scan(&i.OrgID, &i.UserID, &i.CreatedAt)
	return &i, err
}

const insertOrgUser = `-- name: InsertOrgUser :one
INSERT INTO anclax.org_users (org_id, user_id) VALUES ($1, $2) RETURNING org_id, user_id, created_at, updated_at
`

type InsertOrgUserParams struct {
	OrgID  int32
	UserID int32
}

func (q *Queries) InsertOrgUser(ctx context.Context, arg InsertOrgUserParams) (*AnclaxOrgUser, error) {
	row := q.db.QueryRow(ctx, insertOrgUser, arg.OrgID, arg.UserID)
	var i AnclaxOrgUser
	err := row.Scan(
		&i.OrgID,
		&i.UserID,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return &i, err
}

const listOrgs = `-- name: ListOrgs :many
SELECT orgs.id, orgs.name, orgs.tz, orgs.created_at, orgs.updated_at
FROM anclax.org_users 
JOIN anclax.orgs AS orgs ON anclax.org_users.org_id = orgs.id
WHERE anclax.org_users.user_id = $1
`

func (q *Queries) ListOrgs(ctx context.Context, userID int32) ([]*AnclaxOrg, error) {
	rows, err := q.db.Query(ctx, listOrgs, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []*AnclaxOrg
	for rows.Next() {
		var i AnclaxOrg
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Tz,
			&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
}
