import { CoursesResponseDto } from '../dto/courses-response.dto';

/**
 * Transformer class for converting raw course data into the standardized CoursesResponseDto format.
 *
 * This class is responsible for:
 * 1. Mapping database/API field names to DTO property names
 * 2. Type conversion and formatting
 * 3. Providing default values or handling null/undefined cases
 * 4. Ensuring consistent output structure across the application
 *
 * The transformer follows the Data Transfer Object pattern to separate
 * data persistence concerns from API response structures.
 *
 * @example
 * // Transform raw database results to API response format
 * const transformer = new CoursesTransformer();
 * const rawCourses = await courseRepository.find();
 * const apiResponse = transformer.transform(rawCourses);
 *
 * @example
 * // Controller usage
 * @Get()
 * async getCourses() {
 *   const courses = await this.courseService.findAll();
 *   return this.coursesTransformer.transform(courses);
 * }
 */
export class CoursesTransformer {
  /**
   * Transforms an array of raw course objects into an array of CoursesResponseDto objects.
   *
   * @param courses - Array of raw course data (typically from database or external API)
   * @returns {CoursesResponseDto[]} Array of transformed course objects ready for API response
   *
   * @throws {Error} May throw if required fields are missing or invalid
   *
   * @example
   * // Input: Raw database rows with prefix field names
   * const rawData = [{
   *   cm_course_id: 123,
   *   cm_course_name: 'Computer Science',
   *   cm_currency: 'USD',
   *   // ... other fields
   * }];
   *
   * // Output: Clean DTO objects
   * const result = transformer.transform(rawData);
   * // [{
   * //   course_id: 123,
   * //   course_name: 'Computer Science',
   * //   currency: 'USD',
   * //   // ... other properties
   * // }]
   */
  transform(courses: any): CoursesResponseDto[] {
    // Early return for empty input
    if (!courses || !Array.isArray(courses)) {
      return [];
    }

    return courses.map((course: any) => {
      // Create and populate the DTO instance
      const coursesResponse: CoursesResponseDto = {
        // Course identifiers
        course_id: Number(course.cm_course_id),
        institue_id: Number(course.cm_institueId), // Note: Typo - should be 'institute_id'
        course_uid: course.courseid,

        // Course information
        currency: course.cm_currency,
        course_name: course.cm_course_name,
        application_fee: Number(course.cm_application_fee),

        // Financial information (Note: 'tution' typo persists from DTO)
        tution_fee_international: course.cm_tution_fee_inter, // Typo: 'tuition'
        tution_fee_per_duration: course.cm_tution_fee_per, // Typo: 'tuition'

        // Duration information
        duration_type: course.cm_duration_type,
        duration: course.cm_duration,
        max_duration: course.cm_max_duration,

        // URLs
        course_website_url: course.coursewebsiteurl,
        institute_website_url: course.institutewebsiteurl,

        // Institutional identifiers
        college_uid: course.cuid,
        college_id: course.collegeId,

        // Location information
        campus_id: course.campusId,
        city_id: course.city_id,
        state_id: course.state_id,
        country_id: course.country_id,
        campus_name: course.campName,
        campus_association_type_id: course.campusAssociationTypeId,
        country_name: course.country_name,
        city_name: course.ct_city_name,
        state_name: course.st_state_name,

        // Review and status
        review_status: course.reviewStatus,

        // Institute information
        institute_name: course.institueName, // Note: Typo - should be 'institute_name'
        institute_notes: course.instituteNotes,
        institute_type: course.instituteType,

        // Categorization
        study_levels: course.study_levels,
        streams: course.streams,
        sub_streams: course.sub_streams,
        intakes: course.intakes,
      };
      return coursesResponse;
    });
  }
}
