package models

import (
	"fmt"
	"os"

	"odoo-go/pkg/orm"
	"odoo-go/pkg/tools"
)

var translateClient *tools.APIClient

func getTranslateClient() *tools.APIClient {
	if translateClient != nil {
		return translateClient
	}
	apiKey := os.Getenv("GOOGLE_TRANSLATE_API_KEY")
	if apiKey == "" {
		return nil
	}
	translateClient = tools.NewAPIClient("https://translation.googleapis.com", apiKey)
	return translateClient
}

func initGoogleTranslate() {
	// Register a translation model for storing translations + providing RPC methods
	m := orm.NewModel("google.translate", orm.ModelOpts{
		Description: "Google Translation Service",
		Type:        orm.ModelTransient, // No persistent table needed
	})

	m.AddFields(
		orm.Text("source_text", orm.FieldOpts{String: "Source Text"}),
		orm.Char("source_lang", orm.FieldOpts{String: "Source Language", Default: "auto"}),
		orm.Char("target_lang", orm.FieldOpts{String: "Target Language", Default: "de"}),
		orm.Text("translated_text", orm.FieldOpts{String: "Translated Text"}),
	)

	// translate: Translate text from one language to another
	// Usage via RPC: call_kw("google.translate", "translate", [args])
	m.RegisterMethod("translate", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		client := getTranslateClient()
		if client == nil {
			return nil, fmt.Errorf("google_translate: GOOGLE_TRANSLATE_API_KEY not configured")
		}

		text := ""
		targetLang := "de"
		sourceLang := ""

		if len(args) > 0 {
			text, _ = args[0].(string)
		}
		if len(args) > 1 {
			targetLang, _ = args[1].(string)
		}
		if len(args) > 2 {
			sourceLang, _ = args[2].(string)
		}

		if text == "" {
			return nil, fmt.Errorf("google_translate: no text provided")
		}

		params := map[string]string{
			"q":      text,
			"target": targetLang,
			"format": "text",
		}
		if sourceLang != "" && sourceLang != "auto" {
			params["source"] = sourceLang
		}

		var result TranslateResponse
		err := client.GetJSON("/language/translate/v2", params, &result)
		if err != nil {
			return nil, fmt.Errorf("google_translate: API error: %w", err)
		}

		if len(result.Data.Translations) == 0 {
			return nil, fmt.Errorf("google_translate: no translation returned")
		}

		t := result.Data.Translations[0]
		return map[string]interface{}{
			"translated_text":    t.TranslatedText,
			"detected_source":    t.DetectedSourceLanguage,
		}, nil
	})

	// translate_batch: Translate multiple texts at once
	m.RegisterMethod("translate_batch", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		client := getTranslateClient()
		if client == nil {
			return nil, fmt.Errorf("google_translate: GOOGLE_TRANSLATE_API_KEY not configured")
		}

		texts, ok := args[0].([]interface{})
		if !ok || len(texts) == 0 {
			return nil, fmt.Errorf("google_translate: texts array required")
		}
		targetLang := "de"
		if len(args) > 1 {
			targetLang, _ = args[1].(string)
		}

		var results []map[string]interface{}
		for _, t := range texts {
			text, _ := t.(string)
			if text == "" {
				continue
			}

			var result TranslateResponse
			err := client.GetJSON("/language/translate/v2", map[string]string{
				"q":      text,
				"target": targetLang,
				"format": "text",
			}, &result)

			if err != nil || len(result.Data.Translations) == 0 {
				results = append(results, map[string]interface{}{
					"source": text, "translated": text, "error": fmt.Sprintf("%v", err),
				})
				continue
			}

			results = append(results, map[string]interface{}{
				"source":     text,
				"translated": result.Data.Translations[0].TranslatedText,
				"detected":   result.Data.Translations[0].DetectedSourceLanguage,
			})
		}

		return results, nil
	})

	// detect_language: Detect the language of a text
	m.RegisterMethod("detect_language", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		client := getTranslateClient()
		if client == nil {
			return nil, fmt.Errorf("google_translate: GOOGLE_TRANSLATE_API_KEY not configured")
		}

		text := ""
		if len(args) > 0 {
			text, _ = args[0].(string)
		}

		var result DetectResponse
		err := client.GetJSON("/language/translate/v2/detect", map[string]string{
			"q": text,
		}, &result)
		if err != nil {
			return nil, err
		}

		if len(result.Data.Detections) > 0 && len(result.Data.Detections[0]) > 0 {
			d := result.Data.Detections[0][0]
			return map[string]interface{}{
				"language":   d.Language,
				"confidence": d.Confidence,
			}, nil
		}
		return nil, fmt.Errorf("google_translate: language detection failed")
	})

	// supported_languages: List supported languages
	m.RegisterMethod("supported_languages", func(rs *orm.Recordset, args ...interface{}) (interface{}, error) {
		client := getTranslateClient()
		if client == nil {
			return nil, fmt.Errorf("google_translate: GOOGLE_TRANSLATE_API_KEY not configured")
		}

		var result LanguagesResponse
		err := client.GetJSON("/language/translate/v2/languages", map[string]string{
			"target": "de",
		}, &result)
		if err != nil {
			return nil, err
		}

		var langs []map[string]string
		for _, l := range result.Data.Languages {
			langs = append(langs, map[string]string{
				"code": l.Language,
				"name": l.Name,
			})
		}
		return langs, nil
	})
}

// --- Google Translate API Response Types ---

type TranslateResponse struct {
	Data struct {
		Translations []struct {
			TranslatedText         string `json:"translatedText"`
			DetectedSourceLanguage string `json:"detectedSourceLanguage"`
		} `json:"translations"`
	} `json:"data"`
}

type DetectResponse struct {
	Data struct {
		Detections [][]struct {
			Language   string  `json:"language"`
			Confidence float64 `json:"confidence"`
		} `json:"detections"`
	} `json:"data"`
}

type LanguagesResponse struct {
	Data struct {
		Languages []struct {
			Language string `json:"language"`
			Name     string `json:"name"`
		} `json:"languages"`
	} `json:"data"`
}
