import type { Circle } from "@/services/circle";
import type { Component } from "vue";

export type LoginMethod = "local" | "sso";

export type CourseCompletionStatus = "unknown" | "fail" | "success";

export interface BaseCourseWagtailPage {
  readonly id: number;
  readonly title: string;
  readonly slug: string;
  readonly content_type: string;
  readonly translation_key: string;
  readonly frontend_url: string;
  completion_status?: CourseCompletionStatus;
  completion_status_updated_at?: string;
}

export interface CircleLight {
  readonly id: number;
  readonly title: string;
  readonly translation_key: string;
}

export type LearningContent =
  | LearningContentAssignment
  | LearningContentAttendanceCourse
  | LearningContentDocumentList
  | LearningContentFeedback
  | LearningContentLearningModule
  | LearningContentMediaLibrary
  | LearningContentPlaceholder
  | LearningContentRichText
  | LearningContentTest
  | LearningContentVideo;

export type LearningContentType = LearningContent["content_type"];

export interface LearningContentInterface extends BaseCourseWagtailPage {
  readonly content_type: LearningContentType;
  readonly minutes: number;
  readonly description: string;
  readonly content_url: string;
  parentCircle: Circle;
  parentLearningSequence?: LearningSequence;
  parentLearningUnit?: LearningUnit;
  nextLearningContent?: LearningContent;
  previousLearningContent?: LearningContent;
}

export interface LearningContentAssignment extends LearningContentInterface {
  readonly content_type: "learnpath.LearningContentAssignment";
  readonly content_assignment_id: number;
  readonly assignment_type: AssignmentType;
}

export interface LearningContentAttendanceCourse extends LearningContentInterface {
  readonly content_type: "learnpath.LearningContentAttendanceCourse";
}

export interface LearningContentDocument {
  readonly type: "document";
  readonly id: string;
  readonly value: MediaLibraryContentBlockValue;
}

export interface LearningContentDocumentList extends LearningContentInterface {
  readonly content_type: "learnpath.LearningContentDocumentList";
  readonly documents: LearningContentDocument[];
}

export interface LearningContentFeedback extends LearningContentInterface {
  readonly content_type: "learnpath.LearningContentFeedback";
}

export interface LearningContentLearningModule extends LearningContentInterface {
  readonly content_type: "learnpath.LearningContentLearningModule";
}

export interface LearningContentMediaLibrary extends LearningContentInterface {
  readonly content_type: "learnpath.LearningContentMediaLibrary";
}

export interface LearningContentPlaceholder extends LearningContentInterface {
  readonly content_type: "learnpath.LearningContentPlaceholder";
}

export interface LearningContentRichText extends LearningContentInterface {
  readonly content_type: "learnpath.LearningContentRichText";
  readonly text: string;
}

export interface LearningContentTest extends LearningContentInterface {
  readonly content_type: "learnpath.LearningContentTest";
  readonly checkbox_text: string;
}

export interface LearningContentVideo extends LearningContentInterface {
  readonly content_type: "learnpath.LearningContentVideo";
}

export interface LearningUnitPerformanceCriteria extends BaseCourseWagtailPage {
  readonly content_type: "competence.PerformanceCriteria";
  readonly competence_id: string;
  parentLearningSequence?: LearningSequence;
  parentLearningUnit?: LearningUnit;
}

export interface LearningUnit extends BaseCourseWagtailPage {
  readonly content_type: "learnpath.LearningUnit";
  readonly evaluate_url: string;
  readonly course_category: CourseCategory;
  readonly title_hidden: boolean;
  children: LearningUnitPerformanceCriteria[];

  // additional frontend fields
  learningContents: LearningContent[];
  minutes: number;
  parentLearningSequence?: LearningSequence;
  parentCircle?: Circle;
  last?: boolean;
}

export interface LearningSequence extends BaseCourseWagtailPage {
  readonly content_type: "learnpath.LearningSequence";
  icon: string;
  learningUnits: LearningUnit[];
  minutes: number;
}

export type CircleChild = LearningContentInterface | LearningUnit | LearningSequence;

export interface WagtailLearningPath extends BaseCourseWagtailPage {
  readonly content_type: "learnpath.LearningPath";
  course: Course;
  children: LearningPathChild[];
}

export interface CircleGoal {
  type: "goal";
  value: string;
  id: string;
}

export interface CircleJobSituation {
  type: "job_situation";
  value: string;
  id: string;
}

export interface WagtailCircle extends BaseCourseWagtailPage {
  readonly content_type: "learnpath.Circle";
  readonly description: string;
  readonly goal_description: string;
  readonly goals: CircleGoal[];
  readonly job_situation_description: string;
  readonly job_situations: CircleJobSituation[];
  readonly children: CircleChild[];
}

export interface Topic extends BaseCourseWagtailPage {
  readonly content_type: "learnpath.Topic";
  readonly is_visible: boolean;
  circles: Circle[];
}

export type LearningPathChild = Topic | WagtailCircle;

export interface CourseCompletion {
  id: number;
  created_at: string;
  updated_at: string;
  user: number;
  page_key: string;
  page_type: string;
  page_slug: string;
  course_session: number;
  completion_status: CourseCompletionStatus;
  additional_json_data: unknown;
}

export interface CircleDiagramData {
  index: number;
  title: string;
  icon: string;
  startAngle: number;
  endAngle: number;
  arrowStartAngle: number;
  arrowEndAngle: number;
  done: boolean;
}

export interface Course {
  id: number;
  title: string;
  category_name: string;
  slug: string;
}

export interface CourseCategory {
  id: number;
  name: string;
  general: boolean;
}

export type MediaLibraryContentBlockValue = {
  title: string;
  description: string;
  icon_url: string;
  link_display_text: string;
  url: string;
  open_window: boolean;
};

export interface LearnMediaBlock {
  type: "learn_media";
  id: string;
  value: MediaLibraryContentBlockValue;
}

export interface ExternalLinkBlock {
  type: "external_link";
  id: string;
  value: MediaLibraryContentBlockValue;
}

export interface InternalLinkBlock {
  type: "internal_link";
  id: string;
  value: MediaLibraryContentBlockValue;
}

export interface RelativeLinkBlock {
  type: "relative_link";
  id: string;
  value: MediaLibraryContentBlockValue;
}

export type MediaBlock =
  | LearnMediaBlock
  | ExternalLinkBlock
  | InternalLinkBlock
  | RelativeLinkBlock;

export type MediaBlockType = MediaBlock["type"];

export interface MediaContentCollection {
  type: "content_collection";
  value: {
    title: string;
    description: string;
    contents: MediaBlock[];
  };
}

export interface MediaCategoryPage extends BaseCourseWagtailPage {
  content_type: "media_library.MediaCategoryPage";
  overview_icon: string;
  detail_image: string;
  introduction_text: string;
  description_title: string;
  description_text: string;
  items: {
    type: "item";
    value: string;
    id: string;
  }[];
  course_category: CourseCategory;
  body: MediaContentCollection[];
}

export interface MediaLibraryPage extends BaseCourseWagtailPage {
  readonly content_type: "media_library.MediaLibraryPage";
  readonly course: Course;
  readonly children: MediaCategoryPage[];
}

export interface AssignmentPerformanceObjective {
  readonly type: "performance_objective";
  readonly id: string;
  readonly value: {
    text: string;
  };
}

export interface AssignmentTaskBlockExplanation {
  readonly type: "explanation";
  readonly id: string;
  readonly value: {
    readonly text: string;
  };
}

export interface AssignmentTaskBlockUserConfirmation {
  readonly type: "user_confirmation";
  readonly id: string;
  readonly value: {
    readonly text: string;
  };
}

export interface AssignmentTaskBlockUserTextInput {
  readonly type: "user_text_input";
  readonly id: string;
  readonly value: {
    readonly text?: string;
  };
}

export type AssignmentTaskBlock =
  | AssignmentTaskBlockExplanation
  | AssignmentTaskBlockUserConfirmation
  | AssignmentTaskBlockUserTextInput;

export interface AssignmentTask {
  readonly type: "task";
  readonly id: string;
  readonly value: {
    title: string;
    file_submission_required: boolean;
    content: AssignmentTaskBlock[];
  };
}

export interface AssignmentEvaluationSubTask {
  readonly type: "task";
  readonly id: string;
  readonly value: {
    title: string;
    description: string;
    points: number;
  };
}

export interface AssignmentEvaluationTask {
  readonly type: "task";
  readonly id: string;
  readonly value: {
    title: string;
    description: string;
    max_points: number;
    sub_tasks: AssignmentEvaluationSubTask[];
  };
}

export type AssignmentType = "CASEWORK" | "PREP_ASSIGNMENT" | "REFLECTION";

export interface Assignment extends BaseCourseWagtailPage {
  readonly content_type: "assignment.Assignment";
  readonly assignment_type: AssignmentType;
  readonly intro_text: string;
  readonly effort_required: string;
  readonly performance_objectives: AssignmentPerformanceObjective[];
  readonly evaluation_description: string;
  readonly evaluation_document_url: string;
  readonly tasks: AssignmentTask[];
  readonly evaluation_tasks: AssignmentEvaluationTask[];
}

export interface PerformanceCriteria extends BaseCourseWagtailPage {
  readonly content_type: "competence.PerformanceCriteria";
  readonly competence_id: string;
  readonly circle: CircleLight;
  readonly course_category: CourseCategory;
  readonly learning_unit: BaseCourseWagtailPage;
}

export interface CompetencePage extends BaseCourseWagtailPage {
  readonly content_type: "competence.CompetencePage";
  readonly competence_id: string;
  readonly children: PerformanceCriteria[];
}

export interface CompetenceProfilePage extends BaseCourseWagtailPage {
  readonly content_type: "competence.CompetenceProfilePage";
  readonly course: Course;
  readonly circles: CircleLight[];
  readonly children: CompetencePage[];
}

// dropdown
export interface DropdownListItem {
  title: string;
  icon: Component | string;
  data: object;
}

export interface DropdownSelectable {
  id: number | string;
  name: string;
  iconName?: string;
}

export interface CircleExpert {
  user_id: number;
  user_email: string;
  user_first_name: string;
  user_last_name: string;
  circle_id: number;
  circle_slug: string;
  circle_translation_key: string;
}

export interface CircleDocument {
  id: number;
  name: string;
  file_name: string;
  url: string;
  course_session: number;
  learning_sequence: number;
}

export interface CourseSessionAttendanceCourse {
  learningContentId: number;
  start: string;
  end: string;
  location: string;
  trainer: string;
}

export interface CourseSessionAssignmentDetails {
  learningContentId: number;
  submissionDeadlineDateTimeUtc: string;
  evaluationDeadlineDateTimeUtc: string;
}

export interface CourseSession {
  id: number;
  created_at: string;
  updated_at: string;
  course: Course;
  title: string;
  start_date: string;
  end_date: string;
  learning_path_url: string;
  cockpit_url: string;
  competence_url: string;
  course_url: string;
  media_library_url: string;
  attendance_courses: CourseSessionAttendanceCourse[];
  assignment_details_list: CourseSessionAssignmentDetails[];
  documents: CircleDocument[];
  users: CourseSessionUser[];
}

export type Role = "MEMBER" | "EXPERT" | "TUTOR";

export interface CourseSessionUser {
  session_title: string;
  user_id: number;
  first_name: string;
  last_name: string;
  email: string;
  avatar_url: string;
  role: Role;
}

export interface ExpertSessionUser extends CourseSessionUser {
  role: "EXPERT";
  circles: {
    id: number;
    title: string;
    slug: string;
    translation_key: string;
  }[];
}

// document upload
export interface DocumentUploadData {
  file: File | null;
  name: string;
  learningSequence: {
    id: number;
    name: string;
  };
}

// notifications

export type NotificationType = "USER_INTERACTION" | "PROGRESS" | "INFORMATION";

export interface Notification {
  // given by AbstractNotification model
  id: number;
  timestamp: string;
  unread: boolean;
  actor: string | null;
  verb: string;
  target: string | null;
  action_object: string | null;
  // given by Notification model
  notification_type: NotificationType;
  target_url: string | null;
  actor_avatar_url: string | null;
  course: string | null;
}

export type AssignmentCompletionStatus =
  | "unknwown"
  | "IN_PROGRESS"
  | "SUBMITTED"
  | "EVALUATION_IN_PROGRESS"
  | "EVALUATION_SUBMITTED";

export interface UserDataText {
  text: string;
}

export interface UserDataConfirmation {
  confirmation: boolean;
}

export interface ExpertData {
  points?: number;
  text?: string;
}

export interface AssignmentCompletionData {
  // {
  //    "<user_text_input:uuid>": {"user_data": {"text": "some text from user"}},
  //    "<user_confirmation:uuid>": {"user_data": {"confirmation": true}},
  // }
  [key: string]: {
    user_data?: UserDataText | UserDataConfirmation;
    expert_data?: ExpertData;
  };
}

export interface AssignmentCompletion {
  id: number;
  created_at: string;
  updated_at: string;
  submitted_at: string;
  evaluation_submitted_at: string | null;
  assignment_user: number;
  assignment: number;
  course_session: number;
  completion_status: AssignmentCompletionStatus;
  evaluation_user: number | null;
  completion_data: AssignmentCompletionData;
  evaluation_grade: number | null;
}

export type UpsertUserAssignmentCompletion = {
  assignment_id: number;
  course_session_id: number;
  completion_status: AssignmentCompletionStatus;
  completion_data: AssignmentCompletionData;
};

export type EvaluationCompletionData = UpsertUserAssignmentCompletion & {
  assignment_user_id: number;
  evaluation_grade?: number;
  evaluation_points?: number;
};

export interface UserAssignmentCompletionStatus {
  id: number;
  assignment_user_id: number;
  completion_status: AssignmentCompletionStatus;
  evaluation_grade: number | null;
}
