package com.kt.event.participation.presentation.controller;

import com.kt.event.participation.domain.participant.Participant;
import com.kt.event.participation.domain.participant.ParticipantRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 디버깅용 컨트롤러
 */
@Slf4j
@CrossOrigin(origins = "http://localhost:3000")
@RestController
@RequestMapping("/debug")
@RequiredArgsConstructor
public class DebugController {

    private final ParticipantRepository participantRepository;

    /**
     * 중복 참여 체크 테스트
     */
    @GetMapping("/exists/{eventId}/{phoneNumber}")
    public String testExists(@PathVariable String eventId, @PathVariable String phoneNumber) {
        try {
            log.info("디버그: 중복 체크 시작 - eventId: {}, phoneNumber: {}", eventId, phoneNumber);

            boolean exists = participantRepository.existsByEventIdAndPhoneNumber(eventId, phoneNumber);

            log.info("디버그: 중복 체크 결과 - exists: {}", exists);

            long totalCount = participantRepository.count();
            long eventCount = participantRepository.countByEventId(eventId);

            return String.format(
                "eventId: %s, phoneNumber: %s, exists: %s, totalCount: %d, eventCount: %d",
                eventId, phoneNumber, exists, totalCount, eventCount
            );

        } catch (Exception e) {
            log.error("디버그: 예외 발생", e);
            return "ERROR: " + e.getMessage();
        }
    }

    /**
     * 모든 참여자 데이터 조회
     */
    @GetMapping("/participants")
    public String getAllParticipants() {
        try {
            List<Participant> participants = participantRepository.findAll();

            StringBuilder sb = new StringBuilder();
            sb.append("Total participants: ").append(participants.size()).append("\n\n");

            for (Participant p : participants) {
                sb.append(String.format("ID: %s, EventID: %s, Phone: %s, Name: %s\n",
                    p.getParticipantId(), p.getEventId(), p.getPhoneNumber(), p.getName()));
            }

            return sb.toString();

        } catch (Exception e) {
            log.error("디버그: 참여자 조회 예외 발생", e);
            return "ERROR: " + e.getMessage();
        }
    }

    /**
     * 특정 전화번호의 참여 이력 조회
     */
    @GetMapping("/phone/{phoneNumber}")
    public String getByPhoneNumber(@PathVariable String phoneNumber) {
        try {
            List<Participant> participants = participantRepository.findAll();

            StringBuilder sb = new StringBuilder();
            sb.append("Participants with phone: ").append(phoneNumber).append("\n\n");

            int count = 0;
            for (Participant p : participants) {
                if (phoneNumber.equals(p.getPhoneNumber())) {
                    sb.append(String.format("ID: %s, EventID: %s, Name: %s\n",
                        p.getParticipantId(), p.getEventId(), p.getName()));
                    count++;
                }
            }

            if (count == 0) {
                sb.append("No participants found with this phone number.");
            }

            return sb.toString();

        } catch (Exception e) {
            log.error("디버그: 전화번호별 조회 예외 발생", e);
            return "ERROR: " + e.getMessage();
        }
    }
}