// GraphQL operations 文档
// 依据：02-architecture-design.md §4.2 GraphQL 接入、F9 裁决（首次即 GraphQL）
// 端点：POST /api/v1/parent/graphql
// 命名：查询 PascalCase（CurrentUser / MyChildren / ChildSummary ...）
//
// 所有查询/变更与 parent-bff 契约对齐：
// - 查询：currentUser / myChildren / childSummary / childGrades / childAttendance /
//        childHomework / childWeakness / childTrend / myNotifications / myNotificationPreferences
// - 变更：markAsRead / markAllAsRead / updateNotificationPreferences

import { gql } from "urql";

// ===== 查询 =====

export const CURRENT_USER = gql`
  query CurrentUser {
    currentUser {
      id
      email
      name
      roles
      permissions
      dataScope
      schoolId
    }
  }
`;

export const MY_CHILDREN = gql`
  query MyChildren {
    myChildren {
      id
      name
      avatar
      grade
      schoolName
      classId
      className
      isArchived
    }
  }
`;

export const CHILD_SUMMARY = gql`
  query ChildSummary($childId: ID!) {
    childSummary(childId: $childId) {
      childId
      avgScore
      classRank
      classSize
      attendanceRate
      pendingHomeworkCount
      recentGradeTrend
      recentScores {
        examId
        examName
        examDate
        subject
        studentScore
        classAverage
        classMax
        classMin
        gradeLevel
      }
      upcomingEvents {
        id
        type
        title
        dueDate
      }
    }
  }
`;

export const CHILD_GRADES = gql`
  query ChildGrades($childId: ID!, $subject: String, $limit: Int) {
    childGrades(childId: $childId, subject: $subject, limit: $limit) {
      examId
      examName
      examDate
      subject
      studentScore
      classAverage
      classMax
      classMin
      gradeLevel
    }
  }
`;

export const CHILD_ATTENDANCE = gql`
  query ChildAttendance($childId: ID!, $startDate: String!, $endDate: String!) {
    childAttendance(
      childId: $childId
      startDate: $startDate
      endDate: $endDate
    ) {
      id
      date
      status
      note
    }
  }
`;

export const CHILD_HOMEWORK = gql`
  query ChildHomework($childId: ID!, $status: String) {
    childHomework(childId: $childId, status: $status) {
      id
      title
      subject
      className
      assignedDate
      dueDate
      status
      score
      maxScore
      feedback
    }
  }
`;

export const CHILD_WEAKNESS = gql`
  query ChildWeakness($childId: ID!) {
    childWeakness(childId: $childId) {
      id
      knowledgePoint
      masteryLevel
      subject
      recommendation
    }
  }
`;

export const CHILD_TREND = gql`
  query ChildTrend($childId: ID!, $period: TrendPeriod!) {
    childTrend(childId: $childId, period: $period) {
      childId
      period
      dataPoints {
        date
        score
        subject
      }
    }
  }
`;

export const MY_NOTIFICATIONS = gql`
  query MyNotifications($unreadOnly: Boolean, $limit: Int) {
    myNotifications(unreadOnly: $unreadOnly, limit: $limit) {
      id
      childId
      eventType
      title
      body
      read
      createdAt
      actionUrl
      pinned
    }
  }
`;

export const MY_NOTIFICATION_PREFERENCES = gql`
  query MyNotificationPreferences {
    myNotificationPreferences {
      parentId
      preferences
      defaults
      updatedAt
    }
  }
`;

// ===== 变更 =====

export const MARK_AS_READ = gql`
  mutation MarkAsRead($notificationId: ID!) {
    markAsRead(notificationId: $notificationId) {
      id
      read
    }
  }
`;

export const MARK_ALL_AS_READ = gql`
  mutation MarkAllAsRead {
    markAllAsRead {
      count
    }
  }
`;

export const UPDATE_NOTIFICATION_PREFERENCES = gql`
  mutation UpdateNotificationPreferences(
    $parentId: ID!
    $preferences: JSON!
    $defaults: JSON!
  ) {
    updateNotificationPreferences(
      parentId: $parentId
      preferences: $preferences
      defaults: $defaults
    ) {
      parentId
      updatedAt
    }
  }
`;
