package com.tasteby.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * #358 식당 부분 업데이트 DTO.
 * - null = 변경 없음 (toFieldMap에서 제외).
 * - 화이트리스트는 record 필드로 표현 — Jackson SNAKE_CASE 매핑 유지.
 * - URL: http(s) / "NONE" / 빈 문자열만 허용 ("NONE"은 DDG/Naver 매칭 실패 마킹).
 */
public record RestaurantUpdateDTO(
        @Size(min = 1, max = 200)
        String name,

        @Size(max = 500)
        String address,

        @Size(max = 100)
        String region,

        @JsonProperty("cuisine_type")
        @Size(max = 50)
        String cuisineType,

        @JsonProperty("price_range")
        @Min(1) @Max(5)
        Integer priceRange,

        @Size(max = 50)
        String phone,

        @Pattern(regexp = "^(https?://.*|NONE|)$")
        String website,

        @JsonProperty("tabling_url")
        @Pattern(regexp = "^(https?://.*|NONE|)$")
        String tablingUrl,

        @JsonProperty("catchtable_url")
        @Pattern(regexp = "^(https?://.*|NONE|)$")
        String catchtableUrl,

        @DecimalMin("-90.0") @DecimalMax("90.0")
        BigDecimal latitude,

        @DecimalMin("-180.0") @DecimalMax("180.0")
        BigDecimal longitude,

        @JsonProperty("google_place_id")
        @Size(max = 200)
        String googlePlaceId,

        @JsonProperty("business_status")
        @Size(max = 50)
        String businessStatus,

        @DecimalMin("0.0") @DecimalMax("5.0")
        BigDecimal rating,

        @JsonProperty("rating_count")
        @Min(0)
        Integer ratingCount
) {
    /** null이 아닌 필드만 DB 컬럼명 키로 변환. */
    public Map<String, Object> toFieldMap() {
        Map<String, Object> m = new LinkedHashMap<>();
        if (name != null) m.put("name", name);
        if (address != null) m.put("address", address);
        if (region != null) m.put("region", region);
        if (cuisineType != null) m.put("cuisine_type", cuisineType);
        if (priceRange != null) m.put("price_range", priceRange);
        if (phone != null) m.put("phone", phone);
        if (website != null) m.put("website", website);
        if (tablingUrl != null) m.put("tabling_url", tablingUrl);
        if (catchtableUrl != null) m.put("catchtable_url", catchtableUrl);
        if (latitude != null) m.put("latitude", latitude);
        if (longitude != null) m.put("longitude", longitude);
        if (googlePlaceId != null) m.put("google_place_id", googlePlaceId);
        if (businessStatus != null) m.put("business_status", businessStatus);
        if (rating != null) m.put("rating", rating);
        if (ratingCount != null) m.put("rating_count", ratingCount);
        return m;
    }
}
