import { Controller, Get, Query } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiQuery } from '@nestjs/swagger';
import { InjectDataSource } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';
import { Public } from '../../modules/auth/public.decorator';

@ApiTags('mobile_degree')
@Controller('mobile/degree')
export class MobileDegreeController {
  constructor(@InjectDataSource() private readonly dataSource: DataSource) {}

  @Public()
  @Get()
  @ApiOperation({ summary: 'Get all degrees (mobile)' })
  @ApiQuery({
    name: 'educationLevelId',
    required: false,
    type: Number,
    description: 'Filter by education level ID',
  })
  async getDegrees(@Query('educationLevelId') educationLevelId?: string) {
    let query = `SELECT id, education_levels_id, name, status FROM degree WHERE status = 1`;
    const params = [];

    if (educationLevelId) {
      query += ` AND education_levels_id = ?`;
      params.push(Number(educationLevelId));
    }

    query += ` ORDER BY name ASC`;

    const data = await this.dataSource.query(query, params);
    return { success: true, data };
  }
}
