// GraphQL Resolvers（B1 裁决：P2 起 GraphQL）
// P2 核心 5 Query：dashboard / viewports / me / classes / class
// P3+ Query/Mutation：占位返回 null/空 + extensions.warning
// admin 命名空间：占位返回 null/空（P6 实现）
import type { IResolvers } from "@graphql-tools/utils";
import { TeacherService } from "./teacher.service.js";
import type { GraphQLOperationContext } from "../graphql/context.js";
import { BusinessError } from "../shared/errors/application-error.js";

/**
 * 构建 GraphQL Resolvers
 * 依赖 TeacherService（通过闭包注入，避免 NestJS DI 与 Yoga 的集成复杂度）
 */
export function buildResolvers(service: TeacherService): IResolvers {
  return {
    Query: {
      // ===== P2 核心 5 Query =====

      dashboard: async (
        _parent: unknown,
        _args: unknown,
        ctx: GraphQLOperationContext,
      ) => {
        return service.getDashboard(ctx.callCtx);
      },

      viewports: async (
        _parent: unknown,
        _args: unknown,
        ctx: GraphQLOperationContext,
      ) => {
        return service.getViewports(ctx.callCtx);
      },

      me: async (
        _parent: unknown,
        _args: unknown,
        ctx: GraphQLOperationContext,
      ) => {
        return service.getCurrentUser(ctx.callCtx);
      },

      classes: async (
        _parent: unknown,
        _args: unknown,
        ctx: GraphQLOperationContext,
      ) => {
        return service.getClasses(ctx.callCtx);
      },

      class: async (
        _parent: unknown,
        args: { id: string },
        ctx: GraphQLOperationContext,
      ) => {
        return service.getClass(ctx.callCtx, args.id);
      },

      // ===== P3+ Query 占位（返回空 + warning，跨阶段扩展例外）=====

      exams: async (
        _parent: unknown,
        _args: { classId: string },
        _ctx: GraphQLOperationContext,
      ) => {
        throw new BusinessError(
          "exams query not available in P2 (core-edu gRPC not ready, P3+)",
          { phase: "P2", field: "exams", warning: "field_unavailable_in_p2" },
        );
      },

      homework: async (
        _parent: unknown,
        _args: { classId: string },
        _ctx: GraphQLOperationContext,
      ) => {
        throw new BusinessError(
          "homework query not available in P2 (core-edu gRPC not ready, P3+)",
          {
            phase: "P2",
            field: "homework",
            warning: "field_unavailable_in_p2",
          },
        );
      },

      grades: async (
        _parent: unknown,
        _args: { examId: string },
        _ctx: GraphQLOperationContext,
      ) => {
        throw new BusinessError(
          "grades query not available in P2 (core-edu gRPC not ready, P3+)",
          { phase: "P2", field: "grades", warning: "field_unavailable_in_p2" },
        );
      },

      // ===== P4+ Query 占位 =====

      knowledgePath: async (
        _parent: unknown,
        _args: { knowledgePointId: string },
        _ctx: GraphQLOperationContext,
      ) => {
        throw new BusinessError(
          "knowledgePath query not available in P2 (content gRPC not ready, P4+)",
          {
            phase: "P2",
            field: "knowledgePath",
            warning: "field_unavailable_in_p2",
          },
        );
      },

      classPerformance: async (
        _parent: unknown,
        _args: { classId: string },
        _ctx: GraphQLOperationContext,
      ) => {
        throw new BusinessError(
          "classPerformance query not available in P2 (data-ana gRPC not ready, P4+)",
          {
            phase: "P2",
            field: "classPerformance",
            warning: "field_unavailable_in_p2",
          },
        );
      },

      studentWeakness: async (
        _parent: unknown,
        _args: { studentId: string },
        _ctx: GraphQLOperationContext,
      ) => {
        throw new BusinessError(
          "studentWeakness query not available in P2 (data-ana gRPC not ready, P4+)",
          {
            phase: "P2",
            field: "studentWeakness",
            warning: "field_unavailable_in_p2",
          },
        );
      },

      learningTrend: async (
        _parent: unknown,
        _args: { studentId: string; dateRange?: unknown },
        _ctx: GraphQLOperationContext,
      ) => {
        throw new BusinessError(
          "learningTrend query not available in P2 (data-ana gRPC not ready, P4+)",
          {
            phase: "P2",
            field: "learningTrend",
            warning: "field_unavailable_in_p2",
          },
        );
      },

      // ===== P5+ Query 占位 =====

      notifications: async (
        _parent: unknown,
        _args: { unreadOnly?: boolean },
        _ctx: GraphQLOperationContext,
      ) => {
        throw new BusinessError(
          "notifications query not available in P2 (msg gRPC not ready, P5+)",
          {
            phase: "P2",
            field: "notifications",
            warning: "field_unavailable_in_p2",
          },
        );
      },

      // ===== admin 命名空间（president §5.1：P2 预留，P6 实现）=====

      admin: () => {
        // 返回空对象，admin 子字段 resolver 返回 null/空
        return {};
      },
    },

    Mutation: {
      // ===== P3+ Mutation 占位 =====

      createExam: async (
        _parent: unknown,
        _args: unknown,
        _ctx: GraphQLOperationContext,
      ) => {
        throw new BusinessError(
          "createExam mutation not available in P2 (core-edu gRPC not ready, P3+)",
          {
            phase: "P2",
            field: "createExam",
            warning: "field_unavailable_in_p2",
          },
        );
      },

      assignHomework: async (
        _parent: unknown,
        _args: unknown,
        _ctx: GraphQLOperationContext,
      ) => {
        throw new BusinessError(
          "assignHomework mutation not available in P2 (core-edu gRPC not ready, P3+)",
          {
            phase: "P2",
            field: "assignHomework",
            warning: "field_unavailable_in_p2",
          },
        );
      },

      recordGrade: async (
        _parent: unknown,
        _args: unknown,
        _ctx: GraphQLOperationContext,
      ) => {
        throw new BusinessError(
          "recordGrade mutation not available in P2 (core-edu gRPC not ready, P3+)",
          {
            phase: "P2",
            field: "recordGrade",
            warning: "field_unavailable_in_p2",
          },
        );
      },

      // ===== P5+ Mutation 占位 =====

      generateQuestion: async (
        _parent: unknown,
        _args: unknown,
        _ctx: GraphQLOperationContext,
      ) => {
        throw new BusinessError(
          "generateQuestion mutation not available in P2 (ai gRPC not ready, P5+)",
          {
            phase: "P2",
            field: "generateQuestion",
            warning: "field_unavailable_in_p2",
          },
        );
      },

      markNotificationAsRead: async (
        _parent: unknown,
        _args: { id: string },
        _ctx: GraphQLOperationContext,
      ) => {
        throw new BusinessError(
          "markNotificationAsRead mutation not available in P2 (msg gRPC not ready, P5+)",
          {
            phase: "P2",
            field: "markNotificationAsRead",
            warning: "field_unavailable_in_p2",
          },
        );
      },

      // ===== admin 命名空间 Mutation（P2 预留，P6 实现）=====

      admin: () => {
        return {};
      },
    },

    // ===== admin 命名空间子字段 Resolver（P2 全部返回 null/空，P6 实现）=====

    AdminQuery: {
      users: () => null,
      user: () => null,
      roles: () => [],
      role: () => null,
      school: () => null,
      organizations: () => [],
      auditLogs: () => null,
    },

    AdminMutation: {
      createUser: () => {
        throw new BusinessError(
          "admin.createUser not available in P2 (P6 implementation)",
          { phase: "P2", field: "admin.createUser" },
        );
      },
      updateUser: () => {
        throw new BusinessError(
          "admin.updateUser not available in P2 (P6 implementation)",
          { phase: "P2", field: "admin.updateUser" },
        );
      },
      deleteUser: () => {
        throw new BusinessError(
          "admin.deleteUser not available in P2 (P6 implementation)",
          { phase: "P2", field: "admin.deleteUser" },
        );
      },
      createRole: () => {
        throw new BusinessError(
          "admin.createRole not available in P2 (P6 implementation)",
          { phase: "P2", field: "admin.createRole" },
        );
      },
      updateRole: () => {
        throw new BusinessError(
          "admin.updateRole not available in P2 (P6 implementation)",
          { phase: "P2", field: "admin.updateRole" },
        );
      },
      deleteRole: () => {
        throw new BusinessError(
          "admin.deleteRole not available in P2 (P6 implementation)",
          { phase: "P2", field: "admin.deleteRole" },
        );
      },
      updateSchool: () => {
        throw new BusinessError(
          "admin.updateSchool not available in P2 (P6 implementation)",
          { phase: "P2", field: "admin.updateSchool" },
        );
      },
      createOrganization: () => {
        throw new BusinessError(
          "admin.createOrganization not available in P2 (P6 implementation)",
          { phase: "P2", field: "admin.createOrganization" },
        );
      },
      updateOrganization: () => {
        throw new BusinessError(
          "admin.updateOrganization not available in P2 (P6 implementation)",
          { phase: "P2", field: "admin.updateOrganization" },
        );
      },
      deleteOrganization: () => {
        throw new BusinessError(
          "admin.deleteOrganization not available in P2 (P6 implementation)",
          { phase: "P2", field: "admin.deleteOrganization" },
        );
      },
    },

    // ===== 类型字段 Resolver（Class.exams / Class.homework / Exam.grades 延迟加载）=====

    Class: {
      exams: () => [], // P3+ core-edu gRPC
      homework: () => [], // P3+ core-edu gRPC
    },

    Exam: {
      grades: () => [], // P3+ core-edu gRPC
    },

    Homework: {
      submissions: () => [], // P3+ core-edu gRPC
    },
  };
}
