// Code generated by fkit v3.11.0. DO NOT EDIT.
//
// Source: schema/employee/employee.fkit
// Regenerate with: go run ./tools/fkitgen ./schema/employee

package employee

import (
	"context"
	"database/sql"
	"time"
)

// EmployeeRow is the generated row type for table employee.
type EmployeeRow struct {
	ID        string
	Email     string
	FullName  string
	Status    string
	HiredOn   time.Time
	CreatedAt time.Time
	UpdatedAt time.Time
}

// EmployeeCreateRequest is the generated create payload for table employee.
type EmployeeCreateRequest struct {
	Email    string `json:"email"`
	FullName string `json:"fullName"`
	Status   string `json:"status"`
}

// EmployeeUpdateRequest is the generated update payload for table employee.
type EmployeeUpdateRequest struct {
	FullName *string `json:"fullName,omitempty"`
	Status   *string `json:"status,omitempty"`
}

// CreateEmployee inserts one employee row.
func CreateEmployee(ctx context.Context, db *sql.DB, req EmployeeCreateRequest) (EmployeeRow, error) {
	const q = `INSERT INTO employee (email, full_name, status) VALUES ($1, $2, $3) RETURNING *`
	return scanEmployee(db.QueryRowContext(ctx, q, req.Email, req.FullName, req.Status))
}

// GetEmployee selects one employee row by primary key.
func GetEmployee(ctx context.Context, db *sql.DB, id string) (EmployeeRow, error) {
	const q = `SELECT * FROM employee WHERE id = $1`
	return scanEmployee(db.QueryRowContext(ctx, q, id))
}

// UpdateEmployee patches one employee row.
func UpdateEmployee(ctx context.Context, db *sql.DB, id string, req EmployeeUpdateRequest) (EmployeeRow, error) {
	const q = `UPDATE employee SET full_name = COALESCE($2, full_name), status = COALESCE($3, status),
	           updated_at = now() WHERE id = $1 RETURNING *`
	return scanEmployee(db.QueryRowContext(ctx, q, id, req.FullName, req.Status))
}

// DeleteEmployee removes one employee row.
func DeleteEmployee(ctx context.Context, db *sql.DB, id string) error {
	const q = `DELETE FROM employee WHERE id = $1`
	_, err := db.ExecContext(ctx, q, id)
	return err
}

// ListEmployeesByStatus selects employee rows in one status.
func ListEmployeesByStatus(ctx context.Context, db *sql.DB, status string) ([]EmployeeRow, error) {
	const q = `SELECT * FROM employee WHERE status = $1 ORDER BY full_name`
	rows, err := db.QueryContext(ctx, q, status)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var out []EmployeeRow
	for rows.Next() {
		var r EmployeeRow
		if err := rows.Scan(&r.ID, &r.Email, &r.FullName, &r.Status, &r.HiredOn,
			&r.CreatedAt, &r.UpdatedAt); err != nil {
			return nil, err
		}
		out = append(out, r)
	}
	return out, rows.Err()
}

func scanEmployee(row *sql.Row) (EmployeeRow, error) {
	var r EmployeeRow
	err := row.Scan(&r.ID, &r.Email, &r.FullName, &r.Status, &r.HiredOn, &r.CreatedAt, &r.UpdatedAt)
	return r, err
}
