-- 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(sqlc.narg('first_name')::VARCHAR, first_name),
    last_name = COALESCE(sqlc.narg('last_name')::VARCHAR, last_name),
    phone_number = COALESCE(sqlc.narg('phone_number'), phone_number),
    team_role = COALESCE(sqlc.narg('team_role')::VARCHAR, team_role),
    department = COALESCE(sqlc.narg('department'), department),
    job_title = COALESCE(sqlc.narg('job_title'), job_title),
    employment_type = COALESCE(sqlc.narg('employment_type'), employment_type),
    hire_date = COALESCE(sqlc.narg('hire_date'), hire_date),
    profile_picture_url = COALESCE(sqlc.narg('profile_picture_url'), profile_picture_url),
    bio = COALESCE(sqlc.narg('bio'), bio),
    work_phone = COALESCE(sqlc.narg('work_phone'), work_phone),
    emergency_contact = COALESCE(sqlc.narg('emergency_contact'), emergency_contact),
    permissions = COALESCE(sqlc.narg('permissions'), permissions),
    updated_by = @updated_by,
    updated_at = CURRENT_TIMESTAMP
WHERE id = @id;

-- 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: BulkDeactivateTeamMembersByRole :execrows
UPDATE team_members
SET
    status = 'inactive',
    updated_at = CURRENT_TIMESTAMP
WHERE
    team_role = $1
    AND (
        sqlc.narg('exclude_team_member_id')::BIGINT IS NULL
        OR id <> sqlc.narg('exclude_team_member_id')::BIGINT
    )
    AND status = 'active';

-- name: BulkReactivateTeamMembersByRole :execrows
UPDATE team_members
SET
    status = 'active',
    updated_at = CURRENT_TIMESTAMP
WHERE
    team_role = $1
    AND (
        sqlc.narg('exclude_team_member_id')::BIGINT IS NULL
        OR id <> sqlc.narg('exclude_team_member_id')::BIGINT
    )
    AND status = 'inactive';

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