/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2019 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.csv.script;

import com.axelor.apps.hr.db.Employee;
import com.axelor.apps.hr.db.ExtraHours;
import com.axelor.apps.hr.db.repo.EmployeeRepository;
import com.axelor.apps.hr.db.repo.ExtraHoursRepository;
import com.google.inject.Inject;
import java.util.Map;

public class ImportExtraHours {

  @Inject private EmployeeRepository employeeRepository;

  @Inject private ExtraHoursRepository extraHoursRepository;

  public Long importEmployee(String registrationNumber) {
    Employee employee =
        employeeRepository
            .all()
            .filter("self.registrationNumber = ?1", registrationNumber)
            .fetchOne();
    return employee.getId();
  }

  public Object saveExtraHours(Object bean, Map<String, Object> values) {

    if (!(bean instanceof ExtraHours)) {
      System.out.println(
          "Error: Bean is not an instance of ExtraHours. Actual type: "
              + bean.getClass().getName());
      return null;
    }

    ExtraHours extraHour = (ExtraHours) bean;

    if (extraHour == null) {
      System.out.println("Error: Extra hours data is missing.");
      return null;
    }

    // Print the values for debugging purposes
    System.out.println("Extra hours data found: " + extraHour);
    System.out.println("Values map: " + values);

    return extraHoursRepository.save(extraHour);
  }
}
