import { Injectable, Logger } from '@nestjs/common';
import { CreateDesignDto } from './dto/create-design.dto';
import { UpdateDesignDto } from './dto/update-design.dto';
import { PrismaService } from 'src/prisma/prisma.service';

@Injectable()
export class DesignsService {
	constructor(private prisma: PrismaService) {}
	private logger = new Logger(DesignsService.name);
	create(createDesignDto: CreateDesignDto) {
		const transaction = this.prisma.$transaction(async (tx) => {
			this.logger.log({ level: 'info', message: `Design created with ${createDesignDto}`, refCode: '32201' });
			return tx.design.create({ data: createDesignDto });
		});
		return transaction;
	}

	findAll() {
		const transaction = this.prisma.$transaction(async (tx) => {
			// this.logger.log({ level: 'info', message: 'Returning all designs', refCode: '32202' });
			return tx.design.findMany();
		});
		return transaction;
	}

	findByJobId(jobId: number) {
		const transaction = this.prisma.$transaction(async (tx) => {
			// this.logger.log({ level: 'info', message: `Returning all designs for job ${jobId}`, refCode: '32202' });
			return tx.design.findMany({
				where: {
					jobId,
				},
				include: {
					imprints: {
						include: {
							inks: {
								include: {
									ink: true,
								},
							},
							images: true,
						},
					},
					lineItems: {
						include: {
							product: {
								include: {
									category: true,
								},
							},
							sizes: true,
						},
					},
				},
			});
		});
		return transaction;
	}

	findOne(id: number) {
		const transaction = this.prisma.$transaction(async (tx) => {
			// this.logger.log({ level: 'info', message: `Returning one design ${id}`, refCode: '32202' });
			return tx.design.findUnique({
				where: { id },
			});
		});
		return transaction;
	}

	update(id: number, updateDesignDto: UpdateDesignDto) {
		const transaction = this.prisma.$transaction(async (tx) => {
			this.logger.log({ level: 'info', message: `Updating one design ${id} with ${updateDesignDto}`, refCode: '32203' });
			return tx.design.update({
				data: updateDesignDto,
				where: { id },
			});
		});
		return transaction;
	}

	remove(id: number) {
		const transaction = this.prisma.$transaction(async (tx) => {
			this.logger.log({ level: 'info', message: `Deleting one design ${id}`, refCode: '32204' });
			return tx.design.delete({
				where: { id },
			});
		});
		return transaction;
	}
}
