/**
 * @copyright 2026 NHCarrigan
 * @license Naomi's Public License
 * @author Naomi Carrigan
 */

import { Manga, MangaStatus, CreateMangaDto, UpdateMangaDto } from "@library/shared-types";
import { prisma } from "../lib/prisma";
import {
  validateUrl,
  validateRating,
  validateStringLength,
  validateDataUrl,
  MAX_LENGTHS,
} from "../utils/validation";

export class MangaService {
  private prisma = prisma;

  constructor() {}

  /**
   * Validate manga data for security.
   */
  private validateMangaData(data: CreateMangaDto | UpdateMangaDto): void {
    // Validate string lengths
    if (!validateStringLength(data.title, MAX_LENGTHS.TITLE)) {
      throw new Error(`Title must be ${MAX_LENGTHS.TITLE} characters or less.`);
    }
    if (!validateStringLength(data.author, MAX_LENGTHS.AUTHOR)) {
      throw new Error(`Author must be ${MAX_LENGTHS.AUTHOR} characters or less.`);
    }
    if (!validateStringLength(data.notes, MAX_LENGTHS.NOTES)) {
      throw new Error(`Notes must be ${MAX_LENGTHS.NOTES} characters or less.`);
    }
    // Validate rating
    if (!validateRating(data.rating)) {
      throw new Error("Rating must be an integer between 0 and 10.");
    }

    // Validate cover image URL
    if (data.coverImage) {
      if (data.coverImage.startsWith("data:")) {
        const base64Data = data.coverImage.split(",")[1];
        if (!base64Data) {
          throw new Error("Invalid image data URL format.");
        }
        const sizeInBytes = base64Data.length * 0.75;
        if (sizeInBytes > MAX_LENGTHS.DATA_URL) {
          throw new Error("Cover image must be under 5MB.");
        }
        if (!validateDataUrl(data.coverImage)) {
          throw new Error("Invalid image data URL.");
        }
      } else {
        if (!validateStringLength(data.coverImage, MAX_LENGTHS.URL)) {
          throw new Error(`Cover image URL must be ${MAX_LENGTHS.URL} characters or less.`);
        }
        if (!validateUrl(data.coverImage)) {
          throw new Error("Invalid cover image URL. Only http and https URLs are allowed.");
        }
      }
    }

    // Validate tags
    if (data.tags) {
      for (const tag of data.tags) {
        if (!validateStringLength(tag, MAX_LENGTHS.TAGS)) {
          throw new Error(`Each tag must be ${MAX_LENGTHS.TAGS} characters or less.`);
        }
      }
    }

    // Validate link URLs
    if (data.links) {
      for (const link of data.links) {
        if (!validateUrl(link.url)) {
          throw new Error(`Invalid link URL: ${link.title}. Only http and https URLs are allowed.`);
        }
        if (!validateStringLength(link.title, MAX_LENGTHS.TITLE)) {
          throw new Error(`Link title must be ${MAX_LENGTHS.TITLE} characters or less.`);
        }
        if (!validateStringLength(link.url, MAX_LENGTHS.URL)) {
          throw new Error(`Link URL must be ${MAX_LENGTHS.URL} characters or less.`);
        }
      }
    }
  }

  async getAllManga(): Promise<Manga[]> {
    const manga = await this.prisma.manga.findMany({
      orderBy: { updatedAt: "desc" },
    });

    return manga.map((m) => ({
      ...m,
      status: m.status as unknown as MangaStatus,
      dateAdded: m.dateAdded,
      dateStarted: m.dateStarted || undefined,
      dateCompleted: m.dateCompleted || undefined,
      dateFinished: m.dateFinished || undefined,
      tags: m.tags ?? [],
      links: m.links ?? [],
      createdAt: m.createdAt,
      updatedAt: m.updatedAt,
    }));
  }

  async getMangaById(id: string): Promise<Manga | null> {
    const manga = await this.prisma.manga.findUnique({
      where: { id },
    });

    if (!manga) return null;

    return {
      ...manga,
      status: manga.status as unknown as MangaStatus,
      dateAdded: manga.dateAdded,
      dateStarted: manga.dateStarted || undefined,
      dateCompleted: manga.dateCompleted || undefined,
      dateFinished: manga.dateFinished || undefined,
      tags: manga.tags ?? [],
      links: manga.links ?? [],
      createdAt: manga.createdAt,
      updatedAt: manga.updatedAt,
    };
  }

  async createManga(data: CreateMangaDto): Promise<Manga> {
    // Validate input
    this.validateMangaData(data);

    const manga = await this.prisma.manga.create({
      data: {
        ...data,
        status: data.status.toUpperCase() as any,
      },
    });

    return {
      ...manga,
      status: manga.status as unknown as MangaStatus,
      dateAdded: manga.dateAdded,
      dateStarted: manga.dateStarted || undefined,
      dateCompleted: manga.dateCompleted || undefined,
      dateFinished: manga.dateFinished || undefined,
      tags: manga.tags ?? [],
      links: manga.links ?? [],
      createdAt: manga.createdAt,
      updatedAt: manga.updatedAt,
    };
  }

  async updateManga(id: string, data: UpdateMangaDto): Promise<Manga> {
    // Validate input
    this.validateMangaData(data);

    const updateData = { ...data };
    if (updateData.status) {
      updateData.status = updateData.status.toUpperCase() as any;
    }

    const manga = await this.prisma.manga.update({
      where: { id },
      data: updateData,
    });

    return {
      ...manga,
      status: manga.status as unknown as MangaStatus,
      dateAdded: manga.dateAdded,
      dateStarted: manga.dateStarted || undefined,
      dateCompleted: manga.dateCompleted || undefined,
      dateFinished: manga.dateFinished || undefined,
      tags: manga.tags ?? [],
      links: manga.links ?? [],
      createdAt: manga.createdAt,
      updatedAt: manga.updatedAt,
    };
  }

  async deleteManga(id: string): Promise<void> {
    await this.prisma.manga.delete({
      where: { id },
    });
  }
}
