-- 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 *;

-- name: GetTeamMemberByID :one
SELECT * FROM team_members
WHERE id = $1;

-- name: GetTeamMemberByEmail :one
SELECT * FROM team_members
WHERE email = $1
LIMIT 1;

-- 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 = sqlc.narg('team_role') OR sqlc.narg('team_role') IS NULL)
  AND (department = sqlc.narg('department') OR sqlc.narg('department') IS NULL)
  AND (status = sqlc.narg('status') OR sqlc.narg('status') IS NULL)
ORDER BY created_at DESC
LIMIT sqlc.narg('limit')::INT
OFFSET sqlc.narg('offset')::INT;

-- 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 = sqlc.narg('team_role') OR sqlc.narg('team_role') IS NULL)
  AND (status = sqlc.narg('status') OR sqlc.narg('status') IS NULL);

-- 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;

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

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

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

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

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

-- 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;

-- 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;

-- 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;

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