import Elysia, { t } from "elysia";
import { tts_chatterbox } from "../lib/tts_chatterbox";

const HOST = "https://office4-chatterbox.wibudev.com";

const ChatterboxTTS = new Elysia({
    prefix: '/chatterbox-tts',
    detail: {
        tags: ['chatterbox-tts']
    },
}).get('/list-prompt', async () => {
    const res = await fetch(`${HOST}/list-prompt`);
    const data = await res.json();
    return { data };
}, {
    detail: {
        summary: 'List voice prompts',
        description: 'List voice prompts'
    }
}).post(
    '/register-prompt-file',
    async (c) => {
        const file = c.body.file;
        const filename = file.name;

        const buf = await file.arrayBuffer();
        const blob = new Blob([buf], { type: file.type });

        const form = new FormData();
        form.append("prompt", blob, filename);

        const res = await fetch(`${HOST}/register-prompt-file`, {
            method: "POST",
            body: form
        });

        const data = await res.json();

        return { data };
    },
    {
        body: t.Object({
            file: t.File()
        }),
        detail: {
            summary: 'Register voice prompt from file',
            tags: ['chatterbox-tts']
        }
    }
).post(
    '/register-prompt-base64',
    async (c) => {
        const res = await fetch(`${HOST}/register-prompt-base64`, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(c.body)
        });
        return res.json();
    },
    {
        body: t.Object({
            prompt_name: t.String(),
            base64_audio: t.String()
        }),
        detail: {
            summary: 'Register voice prompt via base64'
        }
    }
).post(
    '/delete-prompt',
    async (c) => {
        const res = await fetch(`${HOST}/delete-prompt`, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(c.body)
        });
        return res.json();
    },
    {
        body: t.Object({
            prompt_name: t.String()
        }),
        detail: {
            summary: 'Delete voice prompt',
            description: 'Delete voice prompt',

        }
    }
).post(
    '/rename-prompt',
    async (c) => {
        const res = await fetch(`${HOST}/rename-prompt`, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(c.body)
        });
        return res.json();
    },
    {
        body: t.Object({
            old_name: t.String(),
            new_name: t.String()
        }),
        detail: {
            summary: 'Rename voice prompt',
            description: 'Rename voice prompt'
        }
    }
).post(
    '/tts-async',
    async (c) => {
        const { title, text, prompt } = c.body
        const data = await tts_chatterbox(text, prompt, title)

        return { title, data };
    },
    {
        body: t.Object({
            title: t.String(),
            text: t.String(),
            prompt: t.String()
        }),
        detail: {
            summary: 'Enqueue TTS job',
            description: 'Enqueue TTS job'
        }
    }
).get('/result/:job_id', async (c) => {
    const res = await fetch(`${HOST}/result/${c.params.job_id}`);
    return res.json();
}, {
    detail: {
        summary: 'Get result by job ID',
        description: 'Get result by job ID'
    }
}).get('/list-file', async () => {
    const res = await fetch(`${HOST}/list-file`);
    const data = await res.json();
    return { data };
}, {
    detail: {
        summary: 'List output files',
        description: 'List output files'
    }
}).get('/file/:filename', async (c) => {
    const res = await fetch(`${HOST}/file/${c.params.filename}`);
    const blob = await res.blob();
    return new Response(blob, {
        headers: {
            "Content-Type": "audio/mpeg",
            "Content-Disposition": `attachment; filename="${c.params.filename}"`
        }
    });
}, {
    detail: {
        summary: 'Download file',
        description: 'Download file'
    }
}).delete('/rm/:filename', async (c) => {
    const res = await fetch(`${HOST}/rm/${c.params.filename}`, {
        method: "DELETE"
    });
    return res.json();
}, {
    detail: {
        summary: 'Delete output file',
        description: 'Delete output file'
    }
}).post('/cleanup', async () => {
    const res = await fetch(`${HOST}/cleanup`, {
        method: "POST"
    });
    return res.json();
}, {
    detail: {
        summary: 'Cleanup output folder + jobstore',
        description: 'Cleanup output folder + jobstore'
    }
});


export default ChatterboxTTS;
