import {
  assetFolderSchema,
  assetLinkSchema,
  assetUpdateSchema,
  assetUploadSchema
} from '../forms/schemas.js';
import fs from 'node:fs';
import {
  createAssetFolder,
  createAssetFromUpload,
  createAssetLink,
  deleteAsset,
  deleteAssetFolder,
  deleteAssetLink,
  getAsset,
  getAssetFile,
  listAssetFolders,
  listAssetLinks,
  listAssets,
  updateAsset,
  updateAssetFolder
} from '../models/assetModel.js';

export function assetFolders(req, res) {
  res.json(listAssetFolders(req.user.id));
}

export function createAssetFolderAction(req, res) {
  const parsed = assetFolderSchema.safeParse(req.body);
  if (!parsed.success) return res.status(400).json({ error: 'Invalid asset folder payload' });
  res.status(201).json(createAssetFolder(parsed.data, req.user.id));
}

export function updateAssetFolderAction(req, res) {
  const parsed = assetFolderSchema.partial().safeParse(req.body);
  if (!parsed.success) return res.status(400).json({ error: 'Invalid asset folder payload' });
  const folder = updateAssetFolder(req.params.id, parsed.data, req.user.id);
  if (!folder) return res.status(404).json({ error: 'Asset folder not found' });
  res.json(folder);
}

export function deleteAssetFolderAction(req, res) {
  deleteAssetFolder(req.params.id, req.user.id);
  res.status(204).end();
}

export function assets(req, res) {
  res.json(listAssets(req.user.id));
}

export function createAssetAction(req, res) {
  const parsed = assetUploadSchema.safeParse(req.body);
  if (!parsed.success) {
    if (req.file?.path) fs.rmSync(req.file.path, { force: true });
    return res.status(400).json({ error: 'Invalid asset upload payload' });
  }
  try {
    res.status(201).json(createAssetFromUpload(req.file, parsed.data, req.user.id));
  } catch (error) {
    if (req.file?.path) fs.rmSync(req.file.path, { force: true });
    res.status(400).json({ error: error.message || 'Asset upload failed' });
  }
}

export function getAssetAction(req, res) {
  const asset = getAsset(req.params.id, req.user.id);
  if (!asset) return res.status(404).json({ error: 'Asset not found' });
  res.json(asset);
}

export function updateAssetAction(req, res) {
  const parsed = assetUpdateSchema.safeParse(req.body);
  if (!parsed.success) return res.status(400).json({ error: 'Invalid asset payload' });
  const asset = updateAsset(req.params.id, parsed.data, req.user.id);
  if (!asset) return res.status(404).json({ error: 'Asset not found' });
  res.json(asset);
}

export function deleteAssetAction(req, res) {
  if (!deleteAsset(req.params.id, req.user.id)) return res.status(404).json({ error: 'Asset not found' });
  res.status(204).end();
}

export function viewAssetAction(req, res) {
  const result = getAssetFile(req.params.id, req.user.id);
  if (!result) return res.status(404).json({ error: 'Asset not found' });
  res.type(result.asset.mimeType || 'application/octet-stream');
  res.setHeader('Content-Disposition', `inline; filename="${encodeURIComponent(result.asset.originalName)}"`);
  res.sendFile(result.filePath);
}

export function downloadAssetAction(req, res) {
  const result = getAssetFile(req.params.id, req.user.id);
  if (!result) return res.status(404).json({ error: 'Asset not found' });
  res.download(result.filePath, result.asset.originalName);
}

export function assetLinks(req, res) {
  const links = listAssetLinks(req.params.id, req.user.id);
  if (!links) return res.status(404).json({ error: 'Asset not found' });
  res.json(links);
}

export function createAssetLinkAction(req, res) {
  const parsed = assetLinkSchema.safeParse(req.body);
  if (!parsed.success) return res.status(400).json({ error: 'Invalid asset link payload' });
  const link = createAssetLink(req.params.id, parsed.data, req.user.id);
  if (!link) return res.status(404).json({ error: 'Asset not found' });
  res.status(201).json(link);
}

export function deleteAssetLinkAction(req, res) {
  if (!deleteAssetLink(req.params.linkId, req.user.id)) return res.status(404).json({ error: 'Asset link not found' });
  res.status(204).end();
}
