package com.ikon.projectmanagement.service.impl;

import com.ikon.projectmanagement.dto.request.MeetingRequestDTO;
import com.ikon.projectmanagement.dto.response.MeetingResponseDTO;
import com.ikon.projectmanagement.entity.Meeting;
import com.ikon.projectmanagement.mapper.MeetingMapper;
import com.ikon.projectmanagement.repository.MeetingRepository;
import com.ikon.projectmanagement.service.MeetingService;
import com.ikon.webservice.WebService;

import lombok.RequiredArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class MeetingServiceImpl extends WebService implements MeetingService {

    @Autowired
    private MeetingMapper meetingMapper;

    @Autowired
    private MeetingRepository meetingRepository;

    @Override
    @Transactional
    public MeetingResponseDTO createMeeting(MeetingRequestDTO meetingRequestDTO) {

        UUID accountId = getActiveAccountId();
        UUID createdBy = getCurrentUserId();

        Meeting meeting = meetingMapper.toEntity(meetingRequestDTO);

        meeting.setAccountId(accountId);
        meeting.setCreatedBy(createdBy);

        Meeting savedMeeting = meetingRepository.save(meeting);

        return meetingMapper.toResponseDTO(savedMeeting);
    }

    @Override
    @Transactional(readOnly = true)
    public List<MeetingResponseDTO> getAllMeetings(UUID projectIdentifier) {

        List<Meeting> meetings = meetingRepository.findByProjectIdentifier(projectIdentifier);

        return meetingMapper.toResponseDTOList(meetings);
    }

    @Override
    @Transactional(readOnly = true)
    public List<MeetingResponseDTO> getMeetingsByProjectIdentifier(String projectIdentifier) {

        List<Meeting> meetings = meetingRepository.findByProjectIdentifier(UUID.fromString(projectIdentifier));

        return meetingMapper.toResponseDTOList(meetings);
    }

    @Override
    @Transactional(readOnly = true)
    public MeetingResponseDTO getMeetingById(UUID meetingId) {

        Meeting meeting = meetingRepository.findById(meetingId)
                .orElseThrow(() -> new RuntimeException("Meeting not found with id: " + meetingId));

        return meetingMapper.toResponseDTO(meeting);
    }

    @Override
    @Transactional
    public MeetingResponseDTO updateMeeting(UUID meetingId, MeetingRequestDTO meetingRequestDTO) {

        Meeting existingMeeting = meetingRepository.findById(meetingId)
                .orElseThrow(() -> new RuntimeException("Meeting not found with id: " + meetingId));

        meetingMapper.updateEntityFromDTO(existingMeeting, meetingRequestDTO);

        Meeting updatedMeeting = meetingRepository.save(existingMeeting);

        return meetingMapper.toResponseDTO(updatedMeeting);
    }

    @Override
    @Transactional
    public void deleteMeeting(UUID meetingId) {

        Meeting meeting = meetingRepository.findById(meetingId)
                .orElseThrow(() -> new RuntimeException("Meeting not found with id: " + meetingId));

        meetingRepository.delete(meeting);
    }
}