package com.flightticket.controller;

import com.flightticket.dto.notification.NotificationPreferenceRequest;
import com.flightticket.dto.notification.NotificationPreferenceResponse;
import com.flightticket.dto.notification.NotificationResponse;
import com.flightticket.service.NotificationService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@RestController
@RequestMapping("/api/v1/notifications")
@RequiredArgsConstructor
public class NotificationController {
    
    private final NotificationService notificationService;
    
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<NotificationResponse>> getUserNotifications(@PathVariable UUID userId) {
        log.info("Get notifications request for user: {}", userId);
        List<NotificationResponse> notifications = notificationService.getUserNotifications(userId);
        return ResponseEntity.ok(notifications);
    }
    
    @GetMapping("/user/{userId}/unread")
    public ResponseEntity<List<NotificationResponse>> getUnreadNotifications(@PathVariable UUID userId) {
        log.info("Get unread notifications request for user: {}", userId);
        List<NotificationResponse> notifications = notificationService.getUnreadNotifications(userId);
        return ResponseEntity.ok(notifications);
    }
    
    @GetMapping("/user/{userId}/unread/count")
    public ResponseEntity<Long> getUnreadCount(@PathVariable UUID userId) {
        log.info("Get unread count request for user: {}", userId);
        long count = notificationService.getUnreadCount(userId);
        return ResponseEntity.ok(count);
    }
    
    @PostMapping("/{notificationId}/read")
    public ResponseEntity<Void> markAsRead(@PathVariable UUID notificationId) {
        log.info("Mark notification as read: {}", notificationId);
        notificationService.markAsRead(notificationId);
        return ResponseEntity.ok().build();
    }
    
    @PostMapping("/user/{userId}/read-all")
    public ResponseEntity<Void> markAllAsRead(@PathVariable UUID userId) {
        log.info("Mark all notifications as read for user: {}", userId);
        notificationService.markAllAsRead(userId);
        return ResponseEntity.ok().build();
    }
    
    @GetMapping("/preferences/{userId}")
    public ResponseEntity<List<NotificationPreferenceResponse>> getNotificationPreferences(@PathVariable UUID userId) {
        log.info("Get notification preferences for user: {}", userId);
        List<NotificationPreferenceResponse> preferences = notificationService.getPreferences(userId);
        return ResponseEntity.ok(preferences);
    }
    
    @PutMapping("/preferences/{userId}")
    public ResponseEntity<Void> updateNotificationPreferences(
            @PathVariable UUID userId,
            @Valid @RequestBody List<NotificationPreferenceRequest> preferences) {
        log.info("Update notification preferences for user: {}", userId);
        notificationService.updatePreferences(userId, preferences);
        return ResponseEntity.ok().build();
    }
}
