import { Prisma, type Shop } from "../../src/generated/prisma/client.js";
import { ShopRepository } from "../repositories/ShopRepository.js";

export class ShopService {
    private shopRepository: ShopRepository;

    constructor() {
        this.shopRepository = new ShopRepository();
    }

    /**
     * Get shop settings
     */
    async getShop(): Promise<Shop | null> {
        return this.shopRepository.findFirst();
    }

    /**
     * Create shop if it doesn't exist,
     * otherwise update the existing shop.
     */
    async saveShop(data: Prisma.ShopCreateInput): Promise<Shop> {
        const existing = await this.shopRepository.findFirst();

        if (!existing) {
            return this.shopRepository.create(data);
        }

        return this.shopRepository.update(existing.id, data);
    }
}