package main

import (
	"encoding/json"
	"fmt"
	"os"
	"time"
)

type Customer struct {
	ID              string    `json:"id"`
	CreatedAt       time.Time `json:"createdAt"`
	CustomerName    string    `json:"customerName"`
	IntendedProduct string    `json:"intendedProduct"`
	Version         string    `json:"version"`
	Description     string    `json:"description"`
	Solution        string    `json:"solution"`
	Type            string    `json:"type"`
	Module          string    `json:"module"`
	StatusProgress  string    `json:"statusProgress"`
	Reporter        string    `json:"reporter"`
}

func main() {
	// 读取客户数据
	data, err := os.ReadFile("data/customers.json")
	if err != nil {
		fmt.Printf("读取文件失败: %v\n", err)
		return
	}

	var customers []Customer
	if err := json.Unmarshal(data, &customers); err != nil {
		fmt.Printf("解析JSON失败: %v\n", err)
		return
	}

	fmt.Printf("原始记录数: %d\n", len(customers))

	// 使用map来去重,保留每个客户名称最新的记录
	customerMap := make(map[string]Customer)

	for _, customer := range customers {
		existing, exists := customerMap[customer.CustomerName]
		if !exists || customer.CreatedAt.After(existing.CreatedAt) {
			customerMap[customer.CustomerName] = customer
		}
	}

	// 转换回切片
	deduplicatedCustomers := make([]Customer, 0, len(customerMap))
	for _, customer := range customerMap {
		deduplicatedCustomers = append(deduplicatedCustomers, customer)
	}

	fmt.Printf("去重后记录数: %d\n", len(deduplicatedCustomers))
	fmt.Printf("删除了 %d 条重复记录\n", len(customers)-len(deduplicatedCustomers))

	// 备份原文件
	backupFile := "data/customers.json.backup"
	if err := os.WriteFile(backupFile, data, 0644); err != nil {
		fmt.Printf("备份文件失败: %v\n", err)
		return
	}
	fmt.Printf("原文件已备份到: %s\n", backupFile)

	// 写入去重后的数据
	outputData, err := json.MarshalIndent(deduplicatedCustomers, "", "  ")
	if err != nil {
		fmt.Printf("生成JSON失败: %v\n", err)
		return
	}

	if err := os.WriteFile("data/customers.json", outputData, 0644); err != nil {
		fmt.Printf("写入文件失败: %v\n", err)
		return
	}

	fmt.Println("✅ 去重完成!")
}
