/*
 *  Copyright (c) 2025 John Ahlroos / Devsoap
 *
 *  Creative Commons Attribution-NoDerivatives 4.0 International Public License
 *
 *  By exercising the Licensed Rights (defined below), You accept and agree
 *  to be bound by the terms and conditions of this Creative Commons
 *  Attribution-NoDerivatives 4.0 International Public License ("Public
 *  License"). To the extent this Public License may be interpreted as a
 *  contract, You are granted the Licensed Rights in consideration of Your
 *  acceptance of these terms and conditions, and the Licensor grants You
 *  such rights in consideration of benefits the Licensor receives from
 *  making the Licensed Material available under these terms and
 *  conditions.
 *
 *  Refer to LICENSE file or https://creativecommons.org/licenses/by-nd/4.0 for full license.
 *
 */

package com.devsoap.tincheck.routes;

import com.devsoap.tincheck.routes.requests.TinGenerateRequest;
import com.devsoap.tincheck.routes.requests.TinValidateRequest;
import com.devsoap.tincheck.routes.responses.GenerationResult;
import com.devsoap.tincheck.services.TinCheckService;
import com.devsoap.tincheck.tin.TinGenerator;
import io.github.resilience4j.ratelimiter.RequestNotPermitted;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Error;
import io.micronaut.http.annotation.*;
import io.micronaut.views.View;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.util.Map;

@Slf4j
@Controller
@RequiredArgsConstructor
public class Routes {

    private final TinCheckService service;

    @Get
    @View("index")
    @Produces(MediaType.TEXT_HTML)
    public Map<String,Object> index() {
        return Map.of(
                "countries", service.getCountryMap(),
                "defaultCountry", service.getDefaultCountry().getCountryCode(),
                "defaultValue", service.generateRateLimited(service.getDefaultCountry().getCountryCode(),
                        LocalDate.of(1983, 6,29), TinGenerator.Gender.MALE).value()
        );
    }

    @Post("validate")
    @View("validation-result")
    @Produces(MediaType.TEXT_HTML)
    public HttpResponse<Map<String,String>> validate(@Body @Valid TinValidateRequest request) {
        var result = service.validateRateLimited(request.country(), request.value());
        return HttpResponse.ok(switch (result.result()) {
            case "valid" -> Map.of("class", result.result(), "text", "TIN is valid");
            case "invalid" -> Map.of("class", result.result(), "text", "TIN is invalid");
            default -> Map.of("class", result.result(), "text", "Unknown error");
        });
    }

    @Post("generate")
    public HttpResponse<GenerationResult> generate(@Body @Valid TinGenerateRequest request){
        return HttpResponse.ok(service.generateRateLimited(request.country(), request.dateOfBirth(), TinGenerator.Gender.fromSymbol(request.gender())));
    }

    @Error(exception = IllegalArgumentException.class)
    public HttpResponse<?> handleInvalidParameters(IllegalArgumentException e) {
        log.error("Invalid parameters for request: {}", e.getMessage());
        return HttpResponse.badRequest(e.getMessage()).contentType(MediaType.TEXT_PLAIN);
    }

    @Error(exception = RequestNotPermitted.class)
    public HttpResponse<?> handleRateLimit(RequestNotPermitted e) {
        log.info("Rate limit for request triggered: {}", e.getMessage());
        return HttpResponse.unauthorized().body(Map.of("error",
                "Free validations consumed. Please buy subscription for more validations."));
    }

    /*
    private <T> HttpResponse<T> responseWithCorsHeaders(HttpRequest<?> httpRequest, MutableHttpResponse<T> response) {
        return response
                .header( "Access-Control-Allow-Origin", hostResolver.resolve(httpRequest))
                .header("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
    }

     */
}