import { prisma } from "@/lib/prisma";
import { Prisma } from "@/app/generated/prisma/client";
import type { Vibe } from "@/app/generated/prisma/enums";

export interface PeopleFilters {
  city?: string;
  interest?: string;
  vibe?: Vibe;
}

export const userRepo = {
  async findByEmail(email: string) {
    return prisma.user.findUnique({ where: { email } });
  },

  async findById(id: string) {
    return prisma.user.findUnique({ where: { id } });
  },

  /** Profil publik (tanpa password) untuk halaman akun. */
  async findPublicProfileById(id: string) {
    return prisma.user.findUnique({
      where: { id },
      select: {
        id: true,
        name: true,
        email: true,
        image: true,
        createdAt: true,
      },
    });
  },

  /**
   * Profil sosial publik untuk halaman /u/[id]. JANGAN sertakan field sensitif
   * (email, password, KYC). Hanya yang user pilih untuk dibagikan.
   */
  async findSocialProfileById(id: string) {
    return prisma.user.findUnique({
      where: { id },
      select: {
        id: true,
        name: true,
        image: true,
        createdAt: true,
        profile: {
          select: {
            bio: true,
            city: true,
            interests: true,
            instagram: true,
            vibe: true,
          },
        },
        organizerVerification: { select: { status: true } },
      },
    });
  },

  /**
   * Discovery /people: ambil user yang punya profil sosial terisi (minimal salah
   * satu dari bio/city/interests/vibe). Filter optional by city/interest/vibe.
   * Tidak ekspos email/KYC.
   */
  async findPeople(filters?: PeopleFilters, limit = 60) {
    const profileWhere: Prisma.UserProfileWhereInput = {
      OR: [
        { bio: { not: null } },
        { city: { not: null } },
        { vibe: { not: null } },
        { interests: { isEmpty: false } },
      ],
    };

    if (filters?.city) {
      profileWhere.city = {
        contains: filters.city,
        mode: "insensitive",
      };
    }
    if (filters?.interest) {
      profileWhere.interests = { has: filters.interest.toLowerCase() };
    }
    if (filters?.vibe) {
      profileWhere.vibe = filters.vibe;
    }

    return prisma.user.findMany({
      where: { profile: { is: profileWhere } },
      select: {
        id: true,
        name: true,
        image: true,
        createdAt: true,
        profile: {
          select: {
            bio: true,
            city: true,
            interests: true,
            vibe: true,
          },
        },
        organizerVerification: { select: { status: true } },
      },
      orderBy: { createdAt: "desc" },
      take: limit,
    });
  },

  async create(data: Prisma.UserCreateInput) {
    return prisma.user.create({ data });
  },

  /**
   * Tandai user sudah accept T&C/Privacy. Idempotent: kalau sudah `true`,
   * tidak overwrite `acceptedAt` (audit trail pertama tetap akurat).
   */
  async markAcceptedTerms(id: string) {
    const result = await prisma.user.updateMany({
      where: { id, acceptedTermsAndPrivacy: false },
      data: { acceptedTermsAndPrivacy: true, acceptedAt: new Date() },
    });
    return { updated: result.count > 0 };
  },
};
