import 'package:intl/intl.dart';
import 'package:mobdr/main.dart';

class VisitModel {
  late int id;
  late int id_distrib;
  late int id_etab;
  late int id_visite;
  late String name;
  late int photoCount;
  late String date;
  late String image;
  late String type_visite;
  late String langage;
  late DateTime? date_validation;

  VisitModel({
    required this.id,
    required this.id_distrib,
    required this.id_etab,
    required this.id_visite,
    required this.name,
    required this.photoCount,
    required this.date,
    required this.image,
    required this.type_visite,
    required this.langage,
    required this.date_validation,
  });

  static Future<List<VisitModel>> getTodayVisits() async {
    // Retrieve all today visits from the database
    final visits = await objectbox.getTodayVisit();

    // Map each retrieved visit to VisiteModel
    final visitModelList = visits
        .map((visite) => VisitModel(
            id_distrib: visite.id_distrib_visite,
            id_etab: visite.id_etab,
            id: visite.id,
            id_visite: visite.id_visite,
            name: visite.id_etab.toString() + ' - ' + visite.title,
            photoCount: objectbox.getVisitPhotoCount(visite.id_visite),
            date: DateFormat('EEEE d MMMM HH:mm', 'fr_FR')
                .format(visite.date_visite),
            image: visite.url_photo_principale,
            type_visite: visite.type_visite,
            langage: visite.langage,
            date_validation: visite.date_validation))
        .toList();

    // Return the list of VisiteModel
    return visitModelList;
  }

  static Future<List<VisitModel>> getPreviousVisits() async {
    // Retrieve all previsous visits from the database
    final visits = await objectbox.getPreviousVisit();

    // Map each retrieved visit to VisiteModel
    final visitModelList = visits
        .map((visite) => VisitModel(
            id_distrib: visite.id_distrib_visite,
            id_etab: visite.id_etab,
            id: visite.id,
            id_visite: visite.id_visite,
            name: visite.id_etab.toString() + ' - ' + visite.title,
            photoCount: objectbox.getVisitPhotoCount(visite.id_visite),
            date: DateFormat('EEEE d MMMM HH:mm', 'fr_FR')
                .format(visite.date_visite),
            image: visite.url_photo_principale,
            type_visite: visite.type_visite,
            langage: visite.langage,
            date_validation: visite.date_validation))
        .toList();

    // Return the list of VisiteModel
    return visitModelList;
  }

  static Future<List<VisitModel>> getToSyncVisits() async {
    // Retrieve all visits from the database
    final visits = await objectbox.getAllVisit();

    // Map each retrieved visit to VisiteModel
    final visitModelList = visits
        .map((visite) => VisitModel(
            id_distrib: visite.id_distrib_visite,
            id_etab: visite.id_etab,
            id: visite.id,
            id_visite: visite.id_visite,
            name: visite.id_etab.toString() + ' - ' + visite.title,
            photoCount: objectbox.getVisitPhotoCount(visite.id_visite),
            date: DateFormat('EEEE d MMMM HH:mm', 'fr_FR')
                .format(visite.date_visite),
            image: visite.url_photo_principale,
            type_visite: visite.type_visite,
            langage: visite.langage,
            date_validation: visite.date_validation))
        .where((visit) =>
            visit.photoCount >
            0) // Filter out visits with photoCount equal to 0
        .toList();

    // Return the list of VisiteModel
    return visitModelList;
  }
}
