import { randomUUID } from 'node:crypto';

const author1Id = randomUUID();
const author2Id = randomUUID();
const author3Id = randomUUID();

let authors = [
    { id: author1Id, firstname: 'Philip', lastname: 'Ackermann' },
    { id: author2Id, firstname: 'Robert', lastname: 'Martin' },
    { id: author3Id, firstname: 'Erich', lastname: 'Gamma' }
];

let books = [
    { id: randomUUID(), title: 'JavaScript - das umfassende Handbuch', isbn: '978-3836286299', authorId: author1Id },
    { id: randomUUID(), title: 'Clean Code', isbn: '978-0132350884', authorId: author2Id },
    { id: randomUUID(), title: 'Design Patterns', isbn: '978-0201633610', authorId: author3Id }
];

export const resolvers = {
    Query: {
        books: () => books,
        authors: () => authors,
        book: (_, { title, isbn }) => {
            let result = books;
            if (title) {
                result = result.filter(b => b.title.includes(title));
            }
            if (isbn) {
                result = result.filter(b => b.isbn === isbn);
            }
            return result;
        },
        author: (_, { id }) => authors.find(a => a.id === id),
    },
    Mutation: {
        createAuthor: (_, { firstname, lastname }) => {
            const newAuthor = {
                id: randomUUID(),
                firstname,
                lastname,
            };
            authors.push(newAuthor);
            return newAuthor;
        },
        updateAuthor: (_, { id, firstname, lastname }) => {
            const author = authors.find(a => a.id === id);
            if (!author) return null;
            if (firstname !== undefined) author.firstname = firstname;
            if (lastname !== undefined) author.lastname = lastname;
            return author;
        },
        deleteAuthor: (_, { id }) => {
            const index = authors.findIndex(a => a.id === id);
            if (index === -1) return false;
            // Also cleanup books? The requirements didn't specify cascading delete, 
            // but typically we might handle it. For now keeping it simple as "loose data" is ok.
            authors.splice(index, 1);
            return true;
        },
        createBook: (_, { title, isbn, authorId }) => {
            const newBook = {
                id: randomUUID(),
                title,
                isbn,
                authorId,
            };
            books.push(newBook);
            return newBook;
        },
        updateBook: (_, { id, title, isbn, authorId }) => {
            const book = books.find(b => b.id === id);
            if (!book) return null;
            if (title !== undefined) book.title = title;
            if (isbn !== undefined) book.isbn = isbn;
            if (authorId !== undefined) book.authorId = authorId;
            return book;
        },
        deleteBook: (_, { id }) => {
            const index = books.findIndex(b => b.id === id);
            if (index === -1) return false;
            books.splice(index, 1);
            return true;
        },
    },
    Book: {
        author: (parent) => authors.find(a => a.id === parent.authorId),
    },
};
