import { db } from '../db/db_index';
import { homeBanners } from '../db/schema';
import { desc, eq } from 'drizzle-orm';

export interface Banner {
  id: number;
  name: string;
  imageUrl: string;
  description: string | null;
  productIds: number[] | null;
  redirectUrl: string | null;
  serialNum: number | null;
  isActive: boolean;
  createdAt: Date;
  lastUpdated: Date;
}

export async function getBanners(): Promise<Banner[]> {
  const banners = await db.query.homeBanners.findMany({
    orderBy: desc(homeBanners.createdAt),
  });

  return banners.map((banner) => ({
    id: banner.id,
    name: banner.name,
    imageUrl: banner.imageUrl,
    description: banner.description,
    productIds: banner.productIds || [],
    redirectUrl: banner.redirectUrl,
    serialNum: banner.serialNum,
    isActive: banner.isActive,
    createdAt: banner.createdAt,
    lastUpdated: banner.lastUpdated,
  }));
}

export async function getBannerById(id: number): Promise<Banner | null> {
  const banner = await db.query.homeBanners.findFirst({
    where: eq(homeBanners.id, id),
  });

  if (!banner) return null;

  return {
    id: banner.id,
    name: banner.name,
    imageUrl: banner.imageUrl,
    description: banner.description,
    productIds: banner.productIds || [],
    redirectUrl: banner.redirectUrl,
    serialNum: banner.serialNum,
    isActive: banner.isActive,
    createdAt: banner.createdAt,
    lastUpdated: banner.lastUpdated,
  };
}

export type CreateBannerInput = Omit<Banner, 'id' | 'createdAt' | 'lastUpdated'>;

export async function createBanner(input: CreateBannerInput): Promise<Banner> {
  const [banner] = await db.insert(homeBanners).values({
    name: input.name,
    imageUrl: input.imageUrl,
    description: input.description,
    productIds: input.productIds,
    redirectUrl: input.redirectUrl,
    serialNum: input.serialNum,
    isActive: input.isActive,
  }).returning();

  return {
    id: banner.id,
    name: banner.name,
    imageUrl: banner.imageUrl,
    description: banner.description,
    productIds: banner.productIds || [],
    redirectUrl: banner.redirectUrl,
    serialNum: banner.serialNum,
    isActive: banner.isActive,
    createdAt: banner.createdAt,
    lastUpdated: banner.lastUpdated,
  };
}

export type UpdateBannerInput = Partial<Omit<Banner, 'id' | 'createdAt'>>;

export async function updateBanner(id: number, input: UpdateBannerInput): Promise<Banner> {
  const [banner] = await db.update(homeBanners)
    .set({
      ...input,
      lastUpdated: new Date(),
    })
    .where(eq(homeBanners.id, id))
    .returning();

  return {
    id: banner.id,
    name: banner.name,
    imageUrl: banner.imageUrl,
    description: banner.description,
    productIds: banner.productIds || [],
    redirectUrl: banner.redirectUrl,
    serialNum: banner.serialNum,
    isActive: banner.isActive,
    createdAt: banner.createdAt,
    lastUpdated: banner.lastUpdated,
  };
}

export async function deleteBanner(id: number): Promise<void> {
  await db.delete(homeBanners).where(eq(homeBanners.id, id));
}
