package com.healthcare.ohctech.service.impl;

import com.healthcare.ohctech.dto.SubSectionDto;
import com.healthcare.ohctech.entity.BusinessUnit;
import com.healthcare.ohctech.entity.Department;
import com.healthcare.ohctech.entity.Section;
import com.healthcare.ohctech.entity.SubSection;
import com.healthcare.ohctech.repository.BusinessUnitRepo;
import com.healthcare.ohctech.repository.DepartmentRepo;
import com.healthcare.ohctech.repository.SectionRepo;
import com.healthcare.ohctech.repository.SubSectionRepo;
import com.healthcare.ohctech.service.SubSectionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class SubSectionServiceImpl implements SubSectionService {

    private final SubSectionRepo subSectionRepo;
    private final BusinessUnitRepo businessUnitRepo;
    private final SectionRepo sectionRepo;
    private final DepartmentRepo departmentRepo;

    public SubSectionServiceImpl(SubSectionRepo subSectionRepo, BusinessUnitRepo businessUnitRepo, SectionRepo sectionRepo, DepartmentRepo departmentRepo) {
        this.subSectionRepo = subSectionRepo;
        this.businessUnitRepo = businessUnitRepo;
        this.sectionRepo = sectionRepo;
        this.departmentRepo = departmentRepo;
    }

    @Override
    public SubSection getSubSectionById(Long subSectionId) {
        return subSectionRepo.findById(subSectionId)
                .orElseThrow(() -> new RuntimeException("Sub Section not found for ID: " + subSectionId));
    }

    @Override
    public Page<SubSection> getAllSubSections(Pageable pageable) {
        return subSectionRepo.findAll(pageable);
    }

    @Override
    public void addSubSection(SubSectionDto subSectionDto, Long userId) {
        SubSection subSection = convertToEntity(new SubSection(), subSectionDto);
        subSection.setModifiedBy(userId);
        subSectionRepo.save(subSection);
    }

    @Override
    public void updateSubSection(SubSectionDto subSectionDto, Long userId) {
        Long subSectionId = subSectionDto.id();
        SubSection subSection = subSectionRepo.findById(subSectionId)
                .orElseThrow(() -> new RuntimeException("Sub Section not found for ID: " + subSectionId));

        convertToEntity(subSection, subSectionDto);
        subSection.setModifiedBy(userId);
        subSectionRepo.save(subSection);
    }

    @Override
    public void deleteSubSection(Long subSectionId) {
        SubSection subSection = subSectionRepo.findById(subSectionId)
                .orElseThrow(() -> new RuntimeException("Sub Section not found for ID: " + subSectionId));
        subSectionRepo.delete(subSection);
    }

    private SubSection convertToEntity(SubSection subSection, SubSectionDto subSectionDto) {
        subSection.setSubSectionName(subSectionDto.subSectionName());
        subSection.setSubSectionHeadName(subSectionDto.subSectionHeadName());
        subSection.setSubSectionEmail(subSectionDto.subSectionEmail());

        BusinessUnit businessUnit = businessUnitRepo.findById(subSectionDto.buId())
                .orElseThrow(() -> new RuntimeException("Business Unit not found for ID: " + subSectionDto.buId()));
        subSection.setBusinessUnit(businessUnit);

        Section section = sectionRepo.findById(subSectionDto.sectionId())
                .orElseThrow(() -> new RuntimeException("Section not found for ID: " + subSectionDto.sectionId()));
        subSection.setSection(section);

        Department department = departmentRepo.findById(subSectionDto.deptId())
                .orElseThrow(() -> new RuntimeException("Department not found for ID: " + subSectionDto.deptId()));
        subSection.setDepartment(department);

        return subSection;
    }
}