package com.home4j.resource;

import com.home4j.entity.User;
import com.home4j.entity.UserSettings;
import io.quarkus.security.identity.SecurityIdentity;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

/**
 * 用户设置 REST API
 */
@Path("/api/settings")
@RolesAllowed("ADMIN")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UserSettingsResource {

    @Inject
    SecurityIdentity securityIdentity;

    /**
     * 获取当前用户设置
     */
    @GET
    public Response getSettings() {
        String username = securityIdentity.getPrincipal().getName();
        UserSettings settings = UserSettings.findByUsername(username);
        
        if (settings == null) {
            // 返回默认设置
            return Response.ok(new SettingsResponse()).build();
        }
        
        return Response.ok(SettingsResponse.from(settings)).build();
    }

    /**
     * 更新用户设置
     */
    @PUT
    @Transactional
    public Response updateSettings(SettingsRequest request) {
        String username = securityIdentity.getPrincipal().getName();
        User user = User.findByUsername(username);
        
        if (user == null) {
            return Response.status(Response.Status.NOT_FOUND)
                    .entity(new ErrorResponse("用户不存在"))
                    .build();
        }
        
        UserSettings settings = UserSettings.findByUsername(username);
        
        if (settings == null) {
            settings = new UserSettings();
            settings.user = user;
        }
        
        // 更新设置
        if (request.language != null) {
            settings.language = request.language;
        }
        if (request.bgType != null) {
            settings.bgType = request.bgType;
        }
        if (request.bgColor != null) {
            settings.bgColor = request.bgColor;
        }
        if (request.bgImage != null) {
            settings.bgImage = request.bgImage;
        }
        if (request.defaultView != null) {
            settings.defaultView = request.defaultView;
        }
        if (request.cardDisplayMode != null) {
            settings.cardDisplayMode = request.cardDisplayMode;
        }
        if (request.weatherCity != null) {
            settings.weatherCity = request.weatherCity;
        }
        
        settings.persist();
        
        return Response.ok(SettingsResponse.from(settings)).build();
    }

    // ==================== DTO 类 ====================

    /**
     * 设置请求 DTO
     */
    public static class SettingsRequest {
        public String language;
        public String bgType;
        public String bgColor;
        public String bgImage;
        public String defaultView;
        public String cardDisplayMode;
        public String weatherCity;
    }

    /**
     * 设置响应 DTO
     */
    public static class SettingsResponse {
        public String language = "zh-CN";
        public String bgType = "theme";
        public String bgColor;
        public String bgImage;
        public String defaultView = "card";
        public String cardDisplayMode = "icon";
        public String weatherCity;

        public static SettingsResponse from(UserSettings settings) {
            SettingsResponse response = new SettingsResponse();
            response.language = settings.language != null ? settings.language : "zh-CN";
            response.bgType = settings.bgType != null ? settings.bgType : "theme";
            response.bgColor = settings.bgColor;
            response.bgImage = settings.bgImage;
            response.defaultView = settings.defaultView != null ? settings.defaultView : "card";
            response.cardDisplayMode = settings.cardDisplayMode != null ? settings.cardDisplayMode : "icon";
            response.weatherCity = settings.weatherCity;
            return response;
        }
    }

    /**
     * 错误响应 DTO
     */
    public static class ErrorResponse {
        public boolean success = false;
        public String message;

        public ErrorResponse(String message) {
            this.message = message;
        }
    }
}
