import { PrismaClient, PoliticalStatement as PrismaPoliticalStatement } from '@prisma/client';

const prisma = new PrismaClient();

export type PoliticalStatement = {
    id: number;
    locale: string;
    text: string;
    active: boolean;
    source?: string | null;
};

function mapFromPrisma(stmt: PrismaPoliticalStatement): PoliticalStatement {
    return {
        id: stmt.id,
        locale: stmt.locale,
        text: stmt.text,
        active: stmt.active,
        source: stmt.source,
    };
}

export async function getRandomActiveStatement(locale: string): Promise<PoliticalStatement | null> {
    const safeLocale = ['de', 'en'].includes(locale) ? locale : 'en';
    const all = await prisma.politicalStatement.findMany({
        where: {
            locale: safeLocale,
            active: true,
        },
    });

    if (all.length === 0) {
        return null;
    }

    const index = Math.floor(Math.random() * all.length);
    return mapFromPrisma(all[index]);
}

export async function getAllStatements(locale: string): Promise<PoliticalStatement[]> {
    const safeLocale = ['de', 'en'].includes(locale) ? locale : 'en';
    const all = await prisma.politicalStatement.findMany({
        where: { locale: safeLocale },
        orderBy: { id: 'asc' },
    });
    return all.map(mapFromPrisma);
}

export async function createStatement(locale: string, input: Omit<PoliticalStatement, 'id' | 'locale'>): Promise<PoliticalStatement> {
    const safeLocale = ['de', 'en'].includes(locale) ? locale : 'en';
    const created = await prisma.politicalStatement.create({
        data: {
            locale: safeLocale,
            text: input.text,
            active: input.active ?? true,
            source: input.source ?? null,
        },
    });
    return mapFromPrisma(created);
}

export async function updateStatement(locale: string, id: number, input: Partial<Omit<PoliticalStatement, 'id' | 'locale'>>): Promise<PoliticalStatement | null> {
    const safeLocale = ['de', 'en'].includes(locale) ? locale : 'en';

    // Optional: sicherstellen, dass das Statement zu dieser Locale gehört
    const existing = await prisma.politicalStatement.findUnique({ where: { id } });
    if (!existing || existing.locale !== safeLocale) {
        return null;
    }

    const updated = await prisma.politicalStatement.update({
        where: { id },
        data: {
            text: input.text ?? existing.text,
            active: input.active ?? existing.active,
            source: input.source !== undefined ? input.source : existing.source,
        },
    });

    return mapFromPrisma(updated);
}

export async function deleteStatement(locale: string, id: number): Promise<boolean> {
    const safeLocale = ['de', 'en'].includes(locale) ? locale : 'en';

    const existing = await prisma.politicalStatement.findUnique({ where: { id } });
    if (!existing || existing.locale !== safeLocale) {
        return false;
    }

    await prisma.politicalStatement.delete({ where: { id } });
    return true;
}

