import { ERoleCode } from '../constant';
import { EStudentStatus } from '../type/target';
export const GET_SELECT_QUERY = `
  query getSelectQuery($schoolId: Int!, $schoolYearId: Int!) {
    educational_level(order_by: { id: asc }, where: { deleted_at: { _is_null: true }, school_id: { _eq: $schoolId } }) {
      id
      name
      code
    }
    grade(
      order_by: { id: asc }
      where: {
        deleted_at: { _is_null: true }
        school_id: { _eq: $schoolId }
        educational_level: { deleted_at: { _is_null: true } }
      }
    ) {
      id
      name
      code
      educational_level_id
    }
    classroom(
      order_by: { id: asc }
      where: {
        deleted_at: { _is_null: true }
        school_id: { _eq: $schoolId }
        grade: { educational_level: { deleted_at: { _is_null: true } } }
        school_year_id: { _eq: $schoolYearId }
      }
    ) {
      id
      name
      code
      grade {
        id
        educational_level_id
      }
    }

    department(where: { deleted_at: { _is_null: true }, school_id: { _eq: $schoolId } }) {
      id
      name
    }
  }
`;
const getFieldByTarget = (target, schoolYearId, schoolId) => {
    const fieldByTarget = {
        [ERoleCode.TEACHER]: `
      profile_teacher {
          user_code
          educational_level_group {
            educational_level_map_groups(where:{deleted_at:{_is_null:true}}) {
              educational_level_id
            }
          }
          teacher_subject_assignments(where: {deleted_at:{_is_null:true}}) {
            classroom_teachers(where: {deleted_at:{_is_null:true} classroom:{school_year_id:{_eq: ${schoolYearId}}}}) {
             teacher_type
              classroom {
                id
                grade_id
              }
            }
          }
        } 
    `,
        [ERoleCode.STAFF]: `
        profile_staff {
          user_code
          educational_level_group {
            educational_level_map_groups(where:{deleted_at:{_is_null:true}}) {
              educational_level_id
            }
          }
        }
    `,
        [ERoleCode.STUDENT]: `
       profile_student {
          user_code
          grade {
            id
            name
          }
          program_map_educational_level {
          educational_level {
            code
            name
            id
          }
        }
      }
        classroom_students(
          where: {
            deleted_at: { _is_null: true }
            classroom: {
              deleted_at: { _is_null: true }
              school_year_id: { _eq: ${schoolYearId} }
              school_id: { _eq: ${schoolId} }
            }
          }
        ) {
          id
          classroom {
            id
            name
            grade_id
            grade {
              id
              educational_level_id
            }
            program_id
            school_year_id
            program {
              name
            }
        }
      }
        relationshipUsersByStudentId(order_by: {created_at: desc}, where: {deleted_at: {_is_null: true},  user: {deleted_at: {_is_null: true}}, userByStudentId: {deleted_at: {_is_null: true}}}) {
        parent_id
        relationship_code
        user {
          gender
          full_name
          phone_number
        }
      }
    `,
        [ERoleCode.DEPARTMENT]: '',
        [ERoleCode.PARENT]: '',
    };
    return fieldByTarget[target];
};
const getConditionsByTarget = ({ target }) => {
    if (target === ERoleCode.STAFF) {
        return `
      ${`profile_staff:{
              deleted_at:{_is_null:true}
              is_quit_job:{_eq:false}
            }`}
    `;
    }
    if (target === ERoleCode.STUDENT) {
        return `      
      profile_student: {
        student_status: {_eq: ${EStudentStatus.STUDYING}},
        deleted_at:{_is_null:true}
      }
    `;
    }
    if (target === ERoleCode.TEACHER) {
        return `
      profile_teacher: {
        is_quit_job: { _eq: false }
        deleted_at: { _is_null: true }
      }
    `;
    }
    return '';
};
export const queryListUserBuilder = ({ schoolId, schoolYearId, target }) => `  
query getListUserQuery(
    $roleCode: String!
    $limit: Int!
    $offset: Int!
    $searchKeyword: String!
    $schoolId: Int!
  ) {
    users(
      limit: $limit
      offset: $offset
      order_by: {created_at: desc, id:desc}
      where: {
        ${getConditionsByTarget({ target })}
        deleted_at: {_is_null: true}
        school_id:{_eq: $schoolId}
        user_roles: {
          deleted_at: {_is_null: true}
          role: {role_code: {_eq: $roleCode}}
        }
        _or: [
          {full_name: {_ilike: $searchKeyword}}
          {full_name_unaccent: {_ilike: $searchKeyword}}
        ]
      }
    ) {
      id
      full_name
      email
      phone_number
      ${getFieldByTarget(target, schoolYearId, schoolId)}
       user_roles(where: {deleted_at: {_is_null: true}, is_active: {_eq: true}, role: {school_id: {_eq: $schoolId}}}) {
        role {
          name
        }
      }
    }
  }`;
export const getTargetByIdQuery = `
  query getTargetByIdQuery($id: uuid!) {
    target_inform_target_by_pk(id: $id) {
      target_by_roles(where: {deleted_at: {_is_null: true}}) {
        role_code
        target_educational_levels {
          educational_level {
            id
            name
            code
          }
        }
        target_grades {
          grade {
            id
            name
            code
          }
        }
        target_classrooms {
          classroom {
            id
            name
            code
          }
        }
        target_users {
          user {
            id
            name: full_name
            phone: phone_number
            email
            profile_teacher {
              user_code
            } 
            profile_staff {
              user_code
            }
          }
        }
      }
    }
  }
`;
export const GET_TARGET_BY_ROLE_QUERY = `
  query getTargetByRoleQuery($id: uuid!) {
    user_segment_target_by_pk(id: $id) {
      school_year_id
      target_by_roles(where: {deleted_at: {_is_null: true}}) {
        role {
          role_code
        }
        is_all
        notify_with
        target_educational_levels {
          educational_level {
            id
            name
            code
          }
        }
        target_grades {
          grade {
            id
            name
            code
          }
        }
        target_classrooms {
          classroom {
            id
            name
            code
          }
        }
        target_departments {
          department {
            id
            name
          }
        }
        target_users {
          user {
            id
            name: full_name
            phone: phone_number
            email
            profile_student {
              user_code
              grade {
                id
                name
              }
              program_map_educational_level {
                educational_level {
                  code
                  name
                  id
                }
              }
            }

            profile_teacher {
              user_code
            }
            profile_staff {
              user_code
            }
            relationshipUsersByStudentId(
              order_by: {created_at: desc}
              where: {
                deleted_at: {_is_null: true}
                user: {deleted_at: {_is_null: true}}
                userByStudentId: {deleted_at: {_is_null: true}}
              }
            ) {
              parent_id
              relationship_code
              user {
                gender
                full_name
                phone_number
              }
            }
          }
        }
      }
    }
  }
`;
export const GET_LIST_ROLE_QUERY = `
  query getListRoleQuery($schoolId: Int!) {
    role(where: {deleted_at: {_is_null: true}, school_id: {_eq: $schoolId}}) {
      id
      role_code
    }
  }
`;
export const GET_CLASSROOM_BY_USER_IDS = `
  query getClassroomByUserIds($schoolYearId: Int!, $userIds: [Int!]!) {
  classroom_student(
    where: {
      deleted_at: { _is_null: true }
      user:{id:{_in:$userIds}}
      classroom: {
        deleted_at: { _is_null: true }
        school_year_id: { _eq: $schoolYearId }
      }
    }
  ) {
    user{
      id
    }
    classroom {
      id
      name
      grade_id
      grade {
        id
        educational_level_id
      }
      program_id
      school_year_id
      program {
        name
      }
    }
  }
}
`;
export const FIND_STUDENT_BY_USER_CODE = `
query FindStudentByUserCodes($userCodes: [String!]!, $schoolId: Int!) {
  allCoreStudentPublicProfileStudents(
    filter: {
      deletedAt: { isNull: true }
      userCode: { in: $userCodes }
      schoolId: { equalTo: $schoolId }
    }
  ) {
    pageInfo {
      endCursor
      hasNextPage
      hasPreviousPage
      startCursor
    }
    nodes {
      userCode
      gradeId
      studentId
      coreStudentPublicGradeByGradeIdAndSchoolId {
        name
        id: rowId
      }
      coreStudentPublicUserByStudentIdAndSchoolId {
        coreStudentPublicRelationshipUsersByStudentIdAndSchoolId(
          filter: {
            deletedAt: { isNull: true }
            schoolId: { equalTo: $schoolId }
            coreStudentPublicUserByParentIdAndSchoolId:  {
               deletedAt:  {
                  isNull: true
               }
               schoolId: { equalTo: $schoolId }
            }
          }
        ) {
          nodes {
            parentId
            relationshipCode
            rowId
          }
        }
      }
      coreStudentPublicProgramMapEducationalLevelByProgramEducationalIdAndSchoolId {
        coreStudentPublicEducationalLevelByEducationalLevelIdAndSchoolId {
          code
          name
          id: rowId
        }
      }
    }
  }
}

`;
export const FIND_STAFF_BY_USER_CODE = `
query FindStaffByUserCodes($userCodes: [String!]!, $schoolId: Int!) {
  allCoreAdminPublicUsers(
    filter: {
      deletedAt: { isNull: true }
      schoolId: { equalTo: $schoolId }
      coreAdminPublicProfileStaffByStaffIdAndSchoolId: {
        userCode: { in: $userCodes }
        deletedAt: { isNull: true }
        schoolId: { equalTo: $schoolId }
      }
    }
  ) {
    pageInfo {
      endCursor
      hasNextPage
      hasPreviousPage
      startCursor
    }
    nodes {
      id: rowId
      fullName
      phoneNumber
      emailSecondary
      email
      isUseEmailSecondary
      coreAdminPublicProfileStaffByStaffIdAndSchoolId {
        userCode
      }
    }
  }
}
`;
export const FIND_TEACHER_BY_USER_CODE = `
query FindTeacherByUserCodes($userCodes: [String!]!, $schoolId: Int!) {
  allCoreAdminPublicProfileTeachers(
    filter: {
      deletedAt: { isNull: true }
      userCode: { in: $userCodes }
      schoolId: { equalTo: $schoolId }
    }
  ) {
    pageInfo {
      endCursor
      hasNextPage
      hasPreviousPage
      startCursor
    }
    nodes {
      userCode
      coreAdminPublicEducationalLevelGroupByEducationalLevelGroupIdAndSchoolId {
        id: rowId
        name
        schoolId
      }
      coreAdminPublicUserByTeacherIdAndSchoolId {
        email
        emailSecondary
        fullName
        id: rowId
        phoneNumber
        isUseEmailSecondary
        isEmailValid
        isEmailSecondaryValid
      }
    }
  }
}
  `;
export const FIND_USER_INFO = `
  query findUserInfo($ids: [Int!]!, $schoolId: Int!) {
    allCoreAdminPublicUsers(
      filter: {
        rowId: {in: $ids}
        deletedAt: {isNull: true}
        schoolId: {equalTo: $schoolId}
      }
    ) {
      nodes {
        email
        fullName
        isEmailValid
        isEmailSecondaryValid
        isUseEmailSecondary
        id: rowId
      }
    }
  }
`;
export const FIND_CLASSROOM_BY_USER_IDS = `
  query FindClassroomByUserIds($userIds: [Int!]!, $schoolYearId: Int!, $schoolId: Int!) {
    allCoreAcademicPublicClassroomStudents(
      filter: {
        deletedAt: {isNull: true}
        coreAcademicPublicClassroomByClassroomIdAndSchoolId: {
          schoolYearId: {equalTo: $schoolYearId}
        }
        studentId: { in: $userIds }
        schoolId: { equalTo: $schoolId }
      }
    ) {
      nodes {
        coreAcademicPublicClassroomByClassroomIdAndSchoolId {
          name
          rowId
        }
        studentId
      }
    }
  }
`;
//# sourceMappingURL=query.js.map