import { Injectable, BadRequestException } from '@nestjs/common';
import { writeFile } from 'fs/promises';
import * as fs from 'fs';
import * as path from 'path';
import sizeOf from 'image-size';

@Injectable()
export class MediasService {
  private ROOT_MEDIA_FOLDER = './public';

  async saveBase64Image(
    base64: string,
    filenamePrefix = 'image',
    folder: string,
    maxSizeInMB = 2,
  ): Promise<{
    filename: string;
    filepath: string;
    url: string;
    mimetype: string;
    width?: number;
    height?: number;
    sizeInKB: number;
  }> {
    let mimeType = '';
    let base64Data = '';

    const matches = base64.match(/^data:(.+);base64,(.*)$/);
    if (matches) {
      mimeType = matches[1];
      base64Data = matches[2];
    } else {
      base64Data = base64;
      mimeType = 'image/jpeg';
    }

    const buffer = Buffer.from(base64Data, 'base64');

    const sizeInBytes = buffer.length;
    const sizeInKB = Math.round(sizeInBytes / 1024);
    const sizeInMB = sizeInBytes / (1024 * 1024);
    if (sizeInMB > maxSizeInMB) {
      throw new BadRequestException(`Ảnh vượt quá giới hạn ${maxSizeInMB}MB`);
    }

    const extension = this._getExtensionFromMime(mimeType);
    if (!extension) {
      throw new BadRequestException('Không hỗ trợ loại ảnh này');
    }

    const fullPath = path.join(this.ROOT_MEDIA_FOLDER, folder);
    if (!fs.existsSync(fullPath)) {
      fs.mkdirSync(fullPath, { recursive: true });
    }

    const fileNameOnly = `${filenamePrefix}-${Date.now()}.${extension}`;
    const filepath = path.join(fullPath, fileNameOnly);
    const relativeUrl = path.join(folder, fileNameOnly).replace(/\\/g, '/');

    await writeFile(filepath, buffer);

    let dimensions: { width?: number; height?: number } = {};
    try {
      dimensions = sizeOf(buffer);
    } catch (_) {}

    return {
      filename: fileNameOnly,
      filepath,
      url: `/uploads/${relativeUrl}`, // nếu bạn serve thư mục public tại /uploads
      mimetype: mimeType,
      sizeInKB,
      width: dimensions.width,
      height: dimensions.height,
    };
  }

  async removeAvatar(avatar: string): Promise<void> {
    if (!avatar) return;

    // Luôn xử lý path bắt đầu từ 'medias/avatars'
    const relativePath = path.join('medias/avatars', avatar); // ✅ chính xác từ input: khangpn/avatar-xxx.jpg
    const fullPath = path.join(this.ROOT_MEDIA_FOLDER, relativePath);

    const rootPath = path.resolve(this.ROOT_MEDIA_FOLDER);
    const resolvedFullPath = path.resolve(fullPath);

    if (!resolvedFullPath.startsWith(rootPath)) {
      throw new Error('Invalid: Cannot delete outside the specified directory');
    }

    try {
      // Xóa file avatar
      if (fs.existsSync(fullPath)) {
        await fs.promises.unlink(fullPath);
      }

      // Xóa folder username nếu rỗng (khangpn)
      const usernameFolder = path.dirname(fullPath); // => /public/medias/avatars/khangpn
      const files = await fs.promises.readdir(usernameFolder);
      if (files.length === 0) {
        await fs.promises.rmdir(usernameFolder);
      }
    } catch (error) {
      console.error(`Error when deleting avatar or folder: ${error.message}`);
    }
  }

  private _getExtensionFromMime(mime: string): string | null {
    const map = {
      'image/jpeg': 'jpg',
      'image/png': 'png',
      'image/gif': 'gif',
      'image/webp': 'webp',
    };
    return map[mime] || null;
  }

  private _getDatePath(): string[] {
    const now = new Date();
    const yyyy = now.getFullYear().toString();
    const mm = String(now.getMonth() + 1).padStart(2, '0');
    const dd = String(now.getDate()).padStart(2, '0');
    return [yyyy, mm, dd];
  }
}
