import { Router } from 'express';
import fs from 'fs';
import path from 'path';

const ACCOUNTS_DIR = path.resolve(__dirname, '..', '..', '..', 'accounts');

export const accountsRouter = Router();

function readAccountJson(id: string): Record<string, unknown> | null {
  const p = path.join(ACCOUNTS_DIR, id, 'account.json');
  if (!fs.existsSync(p)) return null;
  return JSON.parse(fs.readFileSync(p, 'utf-8'));
}

function writeAccountJson(id: string, data: Record<string, unknown>): void {
  const dir = path.join(ACCOUNTS_DIR, id);
  if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
  fs.writeFileSync(path.join(dir, 'account.json'), JSON.stringify(data, null, 2), 'utf-8');
}

// List
accountsRouter.get('/', (_req, res) => {
  const dirs = fs.readdirSync(ACCOUNTS_DIR, { withFileTypes: true })
    .filter((d) => d.isDirectory() && !d.name.startsWith('_') && !d.name.startsWith('.'))
    .map((d) => d.name);

  const accounts = dirs
    .map((id) => readAccountJson(id))
    .filter(Boolean);
  res.json(accounts);
});

// Get
accountsRouter.get('/:id', (req, res) => {
  const data = readAccountJson(req.params.id);
  if (!data) return res.status(404).json({ error: 'Account not found' });
  res.json(data);
});

// Create
accountsRouter.post('/', (req, res) => {
  const { id, ...rest } = req.body;
  if (!id) return res.status(400).json({ error: 'id is required' });

  const dir = path.join(ACCOUNTS_DIR, id);
  if (fs.existsSync(dir)) return res.status(409).json({ error: 'Account already exists' });

  const data = {
    id,
    name: rest.name || id,
    description: rest.description || '',
    defaultFormat: rest.defaultFormat || '9:16',
    imageModel: rest.imageModel || 'gemini',
    videoModel: rest.videoModel || 'veo3-fast',
    batchSize: rest.batchSize || 30,
    ttsVoice: rest.ttsVoice || '',
    ttsInstruction: rest.ttsInstruction || '',
    storyboardPrompt: rest.storyboardPrompt || 'prompts/分镜.md',
    imageStylePrompt: rest.imageStylePrompt || 'prompts/图片提示词.md',
    videoStylePrompt: rest.videoStylePrompt || 'prompts/视频提示词.md',
    references: rest.references || [],
    capcut: rest.capcut || {},
  };

  writeAccountJson(id, data);
  res.status(201).json(data);
});

// Update
accountsRouter.put('/:id', (req, res) => {
  const existing = readAccountJson(req.params.id);
  if (!existing) return res.status(404).json({ error: 'Account not found' });

  const merged = { ...existing, ...req.body, id: req.params.id };
  writeAccountJson(req.params.id, merged);
  res.json(merged);
});

// Delete
accountsRouter.delete('/:id', (req, res) => {
  const dir = path.join(ACCOUNTS_DIR, req.params.id);
  if (!fs.existsSync(dir)) return res.status(404).json({ error: 'Account not found' });
  fs.rmSync(dir, { recursive: true });
  res.status(204).send();
});
