import db from '@/db/models';
import type { Campuses } from '@/db/models/campuses';

function toCampusCatalogDto(campus: Campuses) {
  const plain = campus.get({ plain: true });

  return {
    id: plain.id,
    name: plain.name,
    code: plain.code,
    mascot: plain.mascot,
    color: plain.color,
    bgGradient: plain.bgGradient,
    borderColor: plain.borderColor,
    textColor: plain.textColor,
    bgLight: plain.bgLight,
    description: plain.description,
    isOnline: plain.isOnline,
  };
}

class CampusCatalogService {
  static async listActive() {
    const rows = await db.campuses.findAll({
      attributes: [
        'id',
        'name',
        'code',
        'mascot',
        'color',
        'bgGradient',
        'borderColor',
        'textColor',
        'bgLight',
        'description',
        'isOnline',
      ],
      where: { active: true },
      order: [['name', 'ASC']],
    });

    return {
      rows: rows.map(toCampusCatalogDto),
      count: rows.length,
    };
  }
}

export default CampusCatalogService;
