package com.honey.honey.controller;

import com.honey.honey.dto.TransactionDto;
import com.honey.honey.model.UserA;
import com.honey.honey.security.UserContext;
import com.honey.honey.service.TransactionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Controller for transaction history operations.
 */
@Slf4j
@RestController
@RequestMapping("/api/transactions")
@RequiredArgsConstructor
public class TransactionController {

    private final TransactionService transactionService;

    /**
     * Gets transaction history for the current user.
     * Returns 50 transactions per page, ordered by creation time descending (newest first).
     * 
     * @param page Page number (0-indexed, defaults to 0)
     * @param timezone Optional timezone (e.g., "Europe/London"). If not provided, uses UTC.
     * @return Page of transactions
     */
    @GetMapping
    public ResponseEntity<Page<TransactionDto>> getTransactions(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(required = false) String timezone) {
        UserA user = UserContext.get();
        Integer userId = user.getId();
        String languageCode = user.getLanguageCode();
        if (languageCode == null || languageCode.isEmpty() || "XX".equals(languageCode)) {
            languageCode = "EN";
        }
        // Note: languageCode is still used for date formatting (localized "at" word)
        // Transaction type localization is now handled in the frontend
        Page<TransactionDto> transactions = transactionService.getUserTransactions(userId, page, timezone, languageCode);
        return ResponseEntity.ok(transactions);
    }
}



