package com.honey.honey.controller;

import com.honey.honey.dto.ClaimTaskResponse;
import com.honey.honey.dto.DailyBonusStatusDto;
import com.honey.honey.dto.RecentBonusClaimDto;
import com.honey.honey.dto.TaskDto;
import com.honey.honey.model.UserA;
import com.honey.honey.security.UserContext;
import com.honey.honey.service.LocalizationService;
import com.honey.honey.service.TaskService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/tasks")
@RequiredArgsConstructor
public class TaskController {

    private final TaskService taskService;
    private final LocalizationService localizationService;

    /**
     * Gets all tasks for a specific type (referral, follow, other).
     * Includes user progress and claim status.
     */
    @GetMapping
    public List<TaskDto> getTasks(@RequestParam String type) {
        UserA user = UserContext.get();
        String languageCode = user.getLanguageCode();
        if (languageCode == null || languageCode.isEmpty() || "XX".equals(languageCode)) {
            languageCode = "EN";
        }
        return taskService.getTasksByType(user.getId(), type, languageCode);
    }

    /**
     * Gets daily bonus status for the current user.
     * Returns availability status and cooldown time if on cooldown.
     */
    @GetMapping("/daily-bonus")
    public ResponseEntity<DailyBonusStatusDto> getDailyBonusStatus() {
        UserA user = UserContext.get();
        DailyBonusStatusDto status = taskService.getDailyBonusStatus(user.getId());
        return ResponseEntity.ok(status);
    }

    /**
     * Gets the 50 most recent daily bonus claims.
     * Returns claims ordered by claimed_at DESC (most recent first).
     * Includes user avatar URL, screen name, and formatted claim timestamp with timezone and localized "at" word.
     * 
     * @param timezone Optional timezone (e.g., "Europe/Kiev"). If not provided, uses UTC.
     */
    @GetMapping("/daily-bonus/recent-claims")
    public ResponseEntity<List<RecentBonusClaimDto>> getRecentDailyBonusClaims(
            @RequestParam(required = false) String timezone) {
        UserA user = UserContext.get();
        String languageCode = user.getLanguageCode();
        if (languageCode == null || languageCode.isEmpty() || "XX".equals(languageCode)) {
            languageCode = "EN";
        }
        List<RecentBonusClaimDto> claims = taskService.getRecentDailyBonusClaims(timezone, languageCode);
        return ResponseEntity.ok(claims);
    }

    /**
     * Claims a task for the current user.
     * Checks if task is completed and gives reward if applicable.
     * Returns 200 with success status and message.
     */
    @PostMapping("/claim")
    public ResponseEntity<ClaimTaskResponse> claimTask(@RequestBody ClaimTaskRequest request) {
        UserA user = UserContext.get();
        boolean claimed = taskService.claimTask(user.getId(), request.getTaskId());
        
        if (claimed) {
            return ResponseEntity.ok(ClaimTaskResponse.builder()
                    .success(true)
                    .message(localizationService.getMessage("task.message.claimed"))
                    .build());
        } else {
            return ResponseEntity.ok(ClaimTaskResponse.builder()
                    .success(false)
                    .message(localizationService.getMessage("task.message.notCompleted"))
                    .build());
        }
    }

    @Data
    public static class ClaimTaskRequest {
        private Integer taskId;
    }
}

