package com.rj.example;

import com.rj.common.AudioManagementConstants;
import com.rj.entity.AudioManagement;
import com.rj.service.IAudioManagementService;
import com.rj.service.IFileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * 音频文件上传功能使用示例
 * 
 * @author 李中华 ,spllzh
 * @since 2025-08-07
 */
@Component
public class AudioFileUploadExample {

    @Autowired
    private IAudioManagementService audioManagementService;

    @Autowired
    private IFileUploadService fileUploadService;

    /**
     * 示例1：创建录音记录并上传音频文件
     */
    public void exampleCreateAudioWithFile() {
        System.out.println("=== 示例1：创建录音记录并上传音频文件 ===");
        
        try {
            // 1. 创建录音记录
            AudioManagement audio = new AudioManagement();
            audio.setId(UUID.randomUUID().toString());
            audio.setRecordingName("测试录音-" + System.currentTimeMillis());
            audio.setRecordingTime(LocalDateTime.now());
            audio.setSalesId("sales001");
            audio.setSalesName("张三");
            audio.setCustomerId("customer001");
            audio.setCustomerName("李四");
            audio.setCustomerPhone("13800138000");
            audio.setDealershipId("dealership001");
            audio.setDealershipName("北京门店");
            audio.setProjectId("project001");
            audio.setProjectName("汽车销售项目");
            audio.setDuration(new java.math.BigDecimal("5.5"));
            audio.setIntentionLevel("高意向");
            audio.setServiceStatus(AudioManagementConstants.SERVICE_STATUS_IN_SERVICE);
            audio.setCreateTime(LocalDateTime.now());
            audio.setUpdateTime(LocalDateTime.now());

            boolean saved = audioManagementService.save(audio);
            if (saved) {
                System.out.println("录音记录创建成功，ID: " + audio.getId());
                
                // 2. 这里可以上传音频文件（需要实际的MultipartFile）
                System.out.println("音频文件上传URL: POST /api/audio/upload");
                System.out.println("参数: file (音频文件), audioId (" + audio.getId() + ")");
                System.out.println("注意：上传成功后会自动更新录音记录中的文件信息");
                
                // 3. 检查音频文件是否存在
                boolean fileExists = fileUploadService.audioFileExists(audio.getId());
                System.out.println("音频文件是否存在: " + fileExists);
                
                if (fileExists) {
                    String fileUrl = fileUploadService.getAudioFileUrl(audio.getId());
                    System.out.println("音频文件访问URL: " + fileUrl);
                }
            } else {
                System.out.println("录音记录创建失败");
            }
            
        } catch (Exception e) {
            System.err.println("创建录音记录失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 示例2：获取录音的音频文件信息
     */
    public void exampleGetAudioFileInfo() {
        System.out.println("=== 示例2：获取录音的音频文件信息 ===");
        
        // 假设有一个录音ID
        String audioId = "test-audio-id";
        
        try {
            // 检查音频文件是否存在
            boolean fileExists = fileUploadService.audioFileExists(audioId);
            System.out.println("音频文件是否存在: " + fileExists);
            
            if (fileExists) {
                // 获取音频文件URL
                String fileUrl = fileUploadService.getAudioFileUrl(audioId);
                System.out.println("音频文件URL: " + fileUrl);
                
                // 获取录音信息
                AudioManagement audio = audioManagementService.getById(audioId);
                if (audio != null) {
                    System.out.println("录音信息: " + audio.getRecordingName());
                    System.out.println("录音时长: " + audio.getDuration() + " 分钟");
                    System.out.println("客户姓名: " + audio.getCustomerName());
                    System.out.println("销售人员: " + audio.getSalesName());
                }
            } else {
                System.out.println("音频文件不存在，可以上传音频文件");
                System.out.println("上传URL: POST /api/audio/upload");
                System.out.println("参数: file (音频文件), audioId (" + audioId + ")");
            }
            
        } catch (Exception e) {
            System.err.println("获取音频文件信息失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 示例3：删除录音及其音频文件
     */
    public void exampleDeleteAudioWithFile() {
        System.out.println("=== 示例3：删除录音及其音频文件 ===");
        
        String audioId = "test-audio-id";
        
        try {
            // 检查录音是否存在
            AudioManagement audio = audioManagementService.getById(audioId);
            if (audio == null) {
                System.out.println("录音记录不存在: " + audioId);
                return;
            }
            
            System.out.println("准备删除录音: " + audio.getRecordingName());
            
            // 检查音频文件是否存在
            boolean fileExists = fileUploadService.audioFileExists(audioId);
            System.out.println("音频文件是否存在: " + fileExists);
            
            // 删除音频文件
            if (fileExists) {
                boolean fileDeleted = fileUploadService.deleteAudioFile(audioId);
                System.out.println("音频文件删除结果: " + fileDeleted);
            }
            
            // 删除录音记录
            boolean recordDeleted = audioManagementService.removeById(audioId);
            System.out.println("录音记录删除结果: " + recordDeleted);
            
            if (recordDeleted) {
                System.out.println("录音及其音频文件删除成功");
            } else {
                System.out.println("录音删除失败");
            }
            
        } catch (Exception e) {
            System.err.println("删除录音失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 示例4：批量处理音频文件
     */
    public void exampleBatchProcessAudioFiles() {
        System.out.println("=== 示例4：批量处理音频文件 ===");
        
        try {
            // 获取所有录音记录
            var audioList = audioManagementService.list();
            System.out.println("总录音数量: " + audioList.size());
            
            int withFileCount = 0;
            int withoutFileCount = 0;
            
            for (AudioManagement audio : audioList) {
                boolean fileExists = fileUploadService.audioFileExists(audio.getId());
                if (fileExists) {
                    withFileCount++;
                    String fileUrl = fileUploadService.getAudioFileUrl(audio.getId());
                    System.out.println("录音 " + audio.getRecordingName() + " 有音频文件: " + fileUrl);
                } else {
                    withoutFileCount++;
                    System.out.println("录音 " + audio.getRecordingName() + " 没有音频文件");
                }
            }
            
            System.out.println("有音频文件的录音数量: " + withFileCount);
            System.out.println("没有音频文件的录音数量: " + withoutFileCount);
            
        } catch (Exception e) {
            System.err.println("批量处理音频文件失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 示例5：查询包含音频文件信息的录音记录
     */
    public void exampleQueryAudioWithFileInfo() {
        System.out.println("=== 示例5：查询包含音频文件信息的录音记录 ===");
        
        try {
            // 获取所有录音记录
            var audioList = audioManagementService.list();
            System.out.println("总录音数量: " + audioList.size());
            
            for (AudioManagement audio : audioList) {
                System.out.println("录音ID: " + audio.getId());
                System.out.println("录音名称: " + audio.getRecordingName());
                System.out.println("音频文件URL: " + audio.getAudioFileUrl());
                System.out.println("音频文件大小: " + audio.getAudioFileSize() + " 字节");
                System.out.println("原始文件名: " + audio.getAudioFileOriginalName());
                System.out.println("文件扩展名: " + audio.getAudioFileExtension());
                System.out.println("---");
            }
            
        } catch (Exception e) {
            System.err.println("查询录音文件信息失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 运行所有示例
     */
    public void runAllExamples() {
        System.out.println("开始运行音频文件上传功能示例...");
        System.out.println();
        
        try {
            exampleCreateAudioWithFile();
            System.out.println();
            
            exampleGetAudioFileInfo();
            System.out.println();
            
            exampleBatchProcessAudioFiles();
            System.out.println();
            
            exampleQueryAudioWithFileInfo();
            System.out.println();
            
            // 注意：删除示例需要谨慎使用
            // exampleDeleteAudioWithFile();
            
        } catch (Exception e) {
            System.err.println("示例运行失败: " + e.getMessage());
            e.printStackTrace();
        }
        
        System.out.println("示例运行完成！");
    }
}
