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

package dbgen

import (
	"context"

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

const CheckTeamMemberEmailExists = `-- name: CheckTeamMemberEmailExists :one
SELECT EXISTS (
    SELECT 1 FROM team_members WHERE email = $1
) AS email_exists
`

func (q *Queries) CheckTeamMemberEmailExists(ctx context.Context, email string) (bool, error) {
	row := q.db.QueryRow(ctx, CheckTeamMemberEmailExists, email)
	var email_exists bool
	err := row.Scan(&email_exists)
	return email_exists, err
}

const CountTeamMembersByStatus = `-- name: CountTeamMembersByStatus :one
SELECT
    COUNT(*) FILTER (WHERE status = 'active') AS active_count,
    COUNT(*) FILTER (WHERE status = 'inactive') AS inactive_count,
    COUNT(*) FILTER (WHERE status = 'suspended') AS suspended_count,
    COUNT(*) FILTER (WHERE status = 'terminated') AS terminated_count,
    COUNT(*) AS total_count
FROM team_members
`

type CountTeamMembersByStatusRow struct {
	ActiveCount     int64 `json:"active_count"`
	InactiveCount   int64 `json:"inactive_count"`
	SuspendedCount  int64 `json:"suspended_count"`
	TerminatedCount int64 `json:"terminated_count"`
	TotalCount      int64 `json:"total_count"`
}

func (q *Queries) CountTeamMembersByStatus(ctx context.Context) (CountTeamMembersByStatusRow, error) {
	row := q.db.QueryRow(ctx, CountTeamMembersByStatus)
	var i CountTeamMembersByStatusRow
	err := row.Scan(
		&i.ActiveCount,
		&i.InactiveCount,
		&i.SuspendedCount,
		&i.TerminatedCount,
		&i.TotalCount,
	)
	return i, err
}

const CreateTeamMember = `-- name: CreateTeamMember :one
INSERT INTO team_members (
    first_name,
    last_name,
    email,
    phone_number,
    password,
    team_role,
    department,
    job_title,
    employment_type,
    hire_date,
    profile_picture_url,
    bio,
    work_phone,
    emergency_contact,
    status,
    email_verified,
    permissions,
    created_by,
    updated_at
)
VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
    $11, $12, $13, $14, $15, $16, $17, $18, CURRENT_TIMESTAMP
)
RETURNING id, first_name, last_name, email, phone_number, password, team_role, department, job_title, employment_type, hire_date, profile_picture_url, bio, work_phone, emergency_contact, status, email_verified, permissions, last_login, created_by, updated_by, created_at, updated_at
`

type CreateTeamMemberParams struct {
	FirstName         string      `json:"first_name"`
	LastName          string      `json:"last_name"`
	Email             string      `json:"email"`
	PhoneNumber       pgtype.Text `json:"phone_number"`
	Password          []byte      `json:"password"`
	TeamRole          string      `json:"team_role"`
	Department        pgtype.Text `json:"department"`
	JobTitle          pgtype.Text `json:"job_title"`
	EmploymentType    pgtype.Text `json:"employment_type"`
	HireDate          pgtype.Date `json:"hire_date"`
	ProfilePictureUrl pgtype.Text `json:"profile_picture_url"`
	Bio               pgtype.Text `json:"bio"`
	WorkPhone         pgtype.Text `json:"work_phone"`
	EmergencyContact  pgtype.Text `json:"emergency_contact"`
	Status            string      `json:"status"`
	EmailVerified     bool        `json:"email_verified"`
	Permissions       []byte      `json:"permissions"`
	CreatedBy         pgtype.Int8 `json:"created_by"`
}

func (q *Queries) CreateTeamMember(ctx context.Context, arg CreateTeamMemberParams) (TeamMember, error) {
	row := q.db.QueryRow(ctx, CreateTeamMember,
		arg.FirstName,
		arg.LastName,
		arg.Email,
		arg.PhoneNumber,
		arg.Password,
		arg.TeamRole,
		arg.Department,
		arg.JobTitle,
		arg.EmploymentType,
		arg.HireDate,
		arg.ProfilePictureUrl,
		arg.Bio,
		arg.WorkPhone,
		arg.EmergencyContact,
		arg.Status,
		arg.EmailVerified,
		arg.Permissions,
		arg.CreatedBy,
	)
	var i TeamMember
	err := row.Scan(
		&i.ID,
		&i.FirstName,
		&i.LastName,
		&i.Email,
		&i.PhoneNumber,
		&i.Password,
		&i.TeamRole,
		&i.Department,
		&i.JobTitle,
		&i.EmploymentType,
		&i.HireDate,
		&i.ProfilePictureUrl,
		&i.Bio,
		&i.WorkPhone,
		&i.EmergencyContact,
		&i.Status,
		&i.EmailVerified,
		&i.Permissions,
		&i.LastLogin,
		&i.CreatedBy,
		&i.UpdatedBy,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const DeleteTeamMember = `-- name: DeleteTeamMember :exec
DELETE FROM team_members
WHERE id = $1
`

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

const GetAllTeamMembers = `-- name: GetAllTeamMembers :many
SELECT
    COUNT(*) OVER () AS total_count,
    id,
    first_name,
    last_name,
    email,
    phone_number,
    team_role,
    department,
    job_title,
    employment_type,
    hire_date,
    profile_picture_url,
    bio,
    work_phone,
    status,
    email_verified,
    permissions,
    last_login,
    created_at,
    updated_at
FROM team_members
WHERE (team_role = $1 OR $1 IS NULL)
  AND (department = $2 OR $2 IS NULL)
  AND (status = $3 OR $3 IS NULL)
ORDER BY created_at DESC
LIMIT $5::INT
OFFSET $4::INT
`

type GetAllTeamMembersParams struct {
	TeamRole   pgtype.Text `json:"team_role"`
	Department pgtype.Text `json:"department"`
	Status     pgtype.Text `json:"status"`
	Offset     pgtype.Int4 `json:"offset"`
	Limit      pgtype.Int4 `json:"limit"`
}

type GetAllTeamMembersRow struct {
	TotalCount        int64              `json:"total_count"`
	ID                int64              `json:"id"`
	FirstName         string             `json:"first_name"`
	LastName          string             `json:"last_name"`
	Email             string             `json:"email"`
	PhoneNumber       pgtype.Text        `json:"phone_number"`
	TeamRole          string             `json:"team_role"`
	Department        pgtype.Text        `json:"department"`
	JobTitle          pgtype.Text        `json:"job_title"`
	EmploymentType    pgtype.Text        `json:"employment_type"`
	HireDate          pgtype.Date        `json:"hire_date"`
	ProfilePictureUrl pgtype.Text        `json:"profile_picture_url"`
	Bio               pgtype.Text        `json:"bio"`
	WorkPhone         pgtype.Text        `json:"work_phone"`
	Status            string             `json:"status"`
	EmailVerified     bool               `json:"email_verified"`
	Permissions       []byte             `json:"permissions"`
	LastLogin         pgtype.Timestamptz `json:"last_login"`
	CreatedAt         pgtype.Timestamptz `json:"created_at"`
	UpdatedAt         pgtype.Timestamptz `json:"updated_at"`
}

func (q *Queries) GetAllTeamMembers(ctx context.Context, arg GetAllTeamMembersParams) ([]GetAllTeamMembersRow, error) {
	rows, err := q.db.Query(ctx, GetAllTeamMembers,
		arg.TeamRole,
		arg.Department,
		arg.Status,
		arg.Offset,
		arg.Limit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetAllTeamMembersRow
	for rows.Next() {
		var i GetAllTeamMembersRow
		if err := rows.Scan(
			&i.TotalCount,
			&i.ID,
			&i.FirstName,
			&i.LastName,
			&i.Email,
			&i.PhoneNumber,
			&i.TeamRole,
			&i.Department,
			&i.JobTitle,
			&i.EmploymentType,
			&i.HireDate,
			&i.ProfilePictureUrl,
			&i.Bio,
			&i.WorkPhone,
			&i.Status,
			&i.EmailVerified,
			&i.Permissions,
			&i.LastLogin,
			&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 GetTeamMemberByEmail = `-- name: GetTeamMemberByEmail :one
SELECT id, first_name, last_name, email, phone_number, password, team_role, department, job_title, employment_type, hire_date, profile_picture_url, bio, work_phone, emergency_contact, status, email_verified, permissions, last_login, created_by, updated_by, created_at, updated_at FROM team_members
WHERE email = $1
LIMIT 1
`

func (q *Queries) GetTeamMemberByEmail(ctx context.Context, email string) (TeamMember, error) {
	row := q.db.QueryRow(ctx, GetTeamMemberByEmail, email)
	var i TeamMember
	err := row.Scan(
		&i.ID,
		&i.FirstName,
		&i.LastName,
		&i.Email,
		&i.PhoneNumber,
		&i.Password,
		&i.TeamRole,
		&i.Department,
		&i.JobTitle,
		&i.EmploymentType,
		&i.HireDate,
		&i.ProfilePictureUrl,
		&i.Bio,
		&i.WorkPhone,
		&i.EmergencyContact,
		&i.Status,
		&i.EmailVerified,
		&i.Permissions,
		&i.LastLogin,
		&i.CreatedBy,
		&i.UpdatedBy,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const GetTeamMemberByID = `-- name: GetTeamMemberByID :one
SELECT id, first_name, last_name, email, phone_number, password, team_role, department, job_title, employment_type, hire_date, profile_picture_url, bio, work_phone, emergency_contact, status, email_verified, permissions, last_login, created_by, updated_by, created_at, updated_at FROM team_members
WHERE id = $1
`

func (q *Queries) GetTeamMemberByID(ctx context.Context, id int64) (TeamMember, error) {
	row := q.db.QueryRow(ctx, GetTeamMemberByID, id)
	var i TeamMember
	err := row.Scan(
		&i.ID,
		&i.FirstName,
		&i.LastName,
		&i.Email,
		&i.PhoneNumber,
		&i.Password,
		&i.TeamRole,
		&i.Department,
		&i.JobTitle,
		&i.EmploymentType,
		&i.HireDate,
		&i.ProfilePictureUrl,
		&i.Bio,
		&i.WorkPhone,
		&i.EmergencyContact,
		&i.Status,
		&i.EmailVerified,
		&i.Permissions,
		&i.LastLogin,
		&i.CreatedBy,
		&i.UpdatedBy,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const GetTeamMembersByDepartment = `-- name: GetTeamMembersByDepartment :many
SELECT
    id,
    first_name,
    last_name,
    email,
    phone_number,
    team_role,
    department,
    job_title,
    employment_type,
    profile_picture_url,
    status,
    created_at
FROM team_members
WHERE department = $1
  AND status = 'active'
ORDER BY first_name, last_name
`

type GetTeamMembersByDepartmentRow struct {
	ID                int64              `json:"id"`
	FirstName         string             `json:"first_name"`
	LastName          string             `json:"last_name"`
	Email             string             `json:"email"`
	PhoneNumber       pgtype.Text        `json:"phone_number"`
	TeamRole          string             `json:"team_role"`
	Department        pgtype.Text        `json:"department"`
	JobTitle          pgtype.Text        `json:"job_title"`
	EmploymentType    pgtype.Text        `json:"employment_type"`
	ProfilePictureUrl pgtype.Text        `json:"profile_picture_url"`
	Status            string             `json:"status"`
	CreatedAt         pgtype.Timestamptz `json:"created_at"`
}

func (q *Queries) GetTeamMembersByDepartment(ctx context.Context, department pgtype.Text) ([]GetTeamMembersByDepartmentRow, error) {
	rows, err := q.db.Query(ctx, GetTeamMembersByDepartment, department)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetTeamMembersByDepartmentRow
	for rows.Next() {
		var i GetTeamMembersByDepartmentRow
		if err := rows.Scan(
			&i.ID,
			&i.FirstName,
			&i.LastName,
			&i.Email,
			&i.PhoneNumber,
			&i.TeamRole,
			&i.Department,
			&i.JobTitle,
			&i.EmploymentType,
			&i.ProfilePictureUrl,
			&i.Status,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetTeamMembersByRole = `-- name: GetTeamMembersByRole :many
SELECT
    id,
    first_name,
    last_name,
    email,
    phone_number,
    team_role,
    department,
    job_title,
    employment_type,
    profile_picture_url,
    status,
    created_at
FROM team_members
WHERE team_role = $1
  AND status = 'active'
ORDER BY first_name, last_name
`

type GetTeamMembersByRoleRow struct {
	ID                int64              `json:"id"`
	FirstName         string             `json:"first_name"`
	LastName          string             `json:"last_name"`
	Email             string             `json:"email"`
	PhoneNumber       pgtype.Text        `json:"phone_number"`
	TeamRole          string             `json:"team_role"`
	Department        pgtype.Text        `json:"department"`
	JobTitle          pgtype.Text        `json:"job_title"`
	EmploymentType    pgtype.Text        `json:"employment_type"`
	ProfilePictureUrl pgtype.Text        `json:"profile_picture_url"`
	Status            string             `json:"status"`
	CreatedAt         pgtype.Timestamptz `json:"created_at"`
}

func (q *Queries) GetTeamMembersByRole(ctx context.Context, teamRole string) ([]GetTeamMembersByRoleRow, error) {
	rows, err := q.db.Query(ctx, GetTeamMembersByRole, teamRole)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetTeamMembersByRoleRow
	for rows.Next() {
		var i GetTeamMembersByRoleRow
		if err := rows.Scan(
			&i.ID,
			&i.FirstName,
			&i.LastName,
			&i.Email,
			&i.PhoneNumber,
			&i.TeamRole,
			&i.Department,
			&i.JobTitle,
			&i.EmploymentType,
			&i.ProfilePictureUrl,
			&i.Status,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const SearchTeamMembers = `-- name: SearchTeamMembers :many
SELECT
    id,
    first_name,
    last_name,
    email,
    phone_number,
    team_role,
    department,
    job_title,
    employment_type,
    hire_date,
    profile_picture_url,
    bio,
    status,
    email_verified,
    permissions,
    last_login,
    created_at,
    updated_at
FROM team_members
WHERE (
        first_name ILIKE '%' || $1 || '%'
     OR last_name  ILIKE '%' || $1 || '%'
     OR email ILIKE '%' || $1 || '%'
     OR phone_number ILIKE '%' || $1 || '%'
    )
  AND (team_role = $2 OR $2 IS NULL)
  AND (status = $3 OR $3 IS NULL)
`

type SearchTeamMembersParams struct {
	Column1  pgtype.Text `json:"column_1"`
	TeamRole pgtype.Text `json:"team_role"`
	Status   pgtype.Text `json:"status"`
}

type SearchTeamMembersRow struct {
	ID                int64              `json:"id"`
	FirstName         string             `json:"first_name"`
	LastName          string             `json:"last_name"`
	Email             string             `json:"email"`
	PhoneNumber       pgtype.Text        `json:"phone_number"`
	TeamRole          string             `json:"team_role"`
	Department        pgtype.Text        `json:"department"`
	JobTitle          pgtype.Text        `json:"job_title"`
	EmploymentType    pgtype.Text        `json:"employment_type"`
	HireDate          pgtype.Date        `json:"hire_date"`
	ProfilePictureUrl pgtype.Text        `json:"profile_picture_url"`
	Bio               pgtype.Text        `json:"bio"`
	Status            string             `json:"status"`
	EmailVerified     bool               `json:"email_verified"`
	Permissions       []byte             `json:"permissions"`
	LastLogin         pgtype.Timestamptz `json:"last_login"`
	CreatedAt         pgtype.Timestamptz `json:"created_at"`
	UpdatedAt         pgtype.Timestamptz `json:"updated_at"`
}

func (q *Queries) SearchTeamMembers(ctx context.Context, arg SearchTeamMembersParams) ([]SearchTeamMembersRow, error) {
	rows, err := q.db.Query(ctx, SearchTeamMembers, arg.Column1, arg.TeamRole, arg.Status)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []SearchTeamMembersRow
	for rows.Next() {
		var i SearchTeamMembersRow
		if err := rows.Scan(
			&i.ID,
			&i.FirstName,
			&i.LastName,
			&i.Email,
			&i.PhoneNumber,
			&i.TeamRole,
			&i.Department,
			&i.JobTitle,
			&i.EmploymentType,
			&i.HireDate,
			&i.ProfilePictureUrl,
			&i.Bio,
			&i.Status,
			&i.EmailVerified,
			&i.Permissions,
			&i.LastLogin,
			&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 UpdateTeamMember = `-- name: UpdateTeamMember :exec
UPDATE team_members
SET
    first_name = COALESCE($1, first_name),
    last_name = COALESCE($2, last_name),
    phone_number = COALESCE($3, phone_number),
    team_role = COALESCE($4, team_role),
    department = COALESCE($5, department),
    job_title = COALESCE($6, job_title),
    employment_type = COALESCE($7, employment_type),
    hire_date = COALESCE($8, hire_date),
    profile_picture_url = COALESCE($9, profile_picture_url),
    bio = COALESCE($10, bio),
    work_phone = COALESCE($11, work_phone),
    emergency_contact = COALESCE($12, emergency_contact),
    permissions = COALESCE($13, permissions),
    updated_by = $14,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $15
`

type UpdateTeamMemberParams struct {
	FirstName         string      `json:"first_name"`
	LastName          string      `json:"last_name"`
	PhoneNumber       pgtype.Text `json:"phone_number"`
	TeamRole          string      `json:"team_role"`
	Department        pgtype.Text `json:"department"`
	JobTitle          pgtype.Text `json:"job_title"`
	EmploymentType    pgtype.Text `json:"employment_type"`
	HireDate          pgtype.Date `json:"hire_date"`
	ProfilePictureUrl pgtype.Text `json:"profile_picture_url"`
	Bio               pgtype.Text `json:"bio"`
	WorkPhone         pgtype.Text `json:"work_phone"`
	EmergencyContact  pgtype.Text `json:"emergency_contact"`
	Permissions       []byte      `json:"permissions"`
	UpdatedBy         pgtype.Int8 `json:"updated_by"`
	ID                int64       `json:"id"`
}

func (q *Queries) UpdateTeamMember(ctx context.Context, arg UpdateTeamMemberParams) error {
	_, err := q.db.Exec(ctx, UpdateTeamMember,
		arg.FirstName,
		arg.LastName,
		arg.PhoneNumber,
		arg.TeamRole,
		arg.Department,
		arg.JobTitle,
		arg.EmploymentType,
		arg.HireDate,
		arg.ProfilePictureUrl,
		arg.Bio,
		arg.WorkPhone,
		arg.EmergencyContact,
		arg.Permissions,
		arg.UpdatedBy,
		arg.ID,
	)
	return err
}

const UpdateTeamMemberEmailVerified = `-- name: UpdateTeamMemberEmailVerified :exec
UPDATE team_members
SET
    email_verified = $1,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $2
`

type UpdateTeamMemberEmailVerifiedParams struct {
	EmailVerified bool  `json:"email_verified"`
	ID            int64 `json:"id"`
}

func (q *Queries) UpdateTeamMemberEmailVerified(ctx context.Context, arg UpdateTeamMemberEmailVerifiedParams) error {
	_, err := q.db.Exec(ctx, UpdateTeamMemberEmailVerified, arg.EmailVerified, arg.ID)
	return err
}

const UpdateTeamMemberLastLogin = `-- name: UpdateTeamMemberLastLogin :exec
UPDATE team_members
SET
    last_login = CURRENT_TIMESTAMP
WHERE id = $1
`

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

const UpdateTeamMemberPassword = `-- name: UpdateTeamMemberPassword :exec
UPDATE team_members
SET
    password = $1,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $2
`

type UpdateTeamMemberPasswordParams struct {
	Password []byte `json:"password"`
	ID       int64  `json:"id"`
}

func (q *Queries) UpdateTeamMemberPassword(ctx context.Context, arg UpdateTeamMemberPasswordParams) error {
	_, err := q.db.Exec(ctx, UpdateTeamMemberPassword, arg.Password, arg.ID)
	return err
}

const UpdateTeamMemberStatus = `-- name: UpdateTeamMemberStatus :exec
UPDATE team_members
SET
    status = $1,
    updated_by = $2,
    updated_at = CURRENT_TIMESTAMP
WHERE id = $3
`

type UpdateTeamMemberStatusParams struct {
	Status    string      `json:"status"`
	UpdatedBy pgtype.Int8 `json:"updated_by"`
	ID        int64       `json:"id"`
}

func (q *Queries) UpdateTeamMemberStatus(ctx context.Context, arg UpdateTeamMemberStatusParams) error {
	_, err := q.db.Exec(ctx, UpdateTeamMemberStatus, arg.Status, arg.UpdatedBy, arg.ID)
	return err
}
