"use server";

import { revalidatePath } from "next/cache";
import { api } from "../../../convex/_generated/api";
import { m } from "@/lib/convex-server";
import { run, type ActionResult } from "@/lib/action";
import type { APARTMENT_STATUSES, CLEANING_STATUSES } from "@/lib/domain";

export interface ApartmentInput {
  code: string;
  name: string;
  building?: string | null;
  floor?: string | null;
  address: string;
  city: string;
  bedrooms: number;
  beds: number;
  bathrooms: number;
  maxGuests: number;
  basePrice: number;
  weekendPrice?: number | null;
  amenities?: string[];
  notes?: string | null;
  isActive?: boolean;
}

const clean = (i: ApartmentInput) => ({ code: i.code, name: i.name, building: i.building ?? null, floor: i.floor ?? null, address: i.address, city: i.city, bedrooms: Number(i.bedrooms), beds: Number(i.beds), bathrooms: Number(i.bathrooms), maxGuests: Number(i.maxGuests), basePrice: Number(i.basePrice), weekendPrice: i.weekendPrice === null || i.weekendPrice === undefined || (i.weekendPrice as unknown) === "" ? null : Number(i.weekendPrice), amenities: i.amenities ?? [], notes: i.notes ?? null, isActive: i.isActive ?? true });
const rev = (id?: string) => {
  for (const p of ["/apartments", "/calendar", "/dashboard", "/cleaning", "/settings/apartments"]) revalidatePath(p);
  if (id) revalidatePath(`/apartments/${id}`);
};

export async function createApartment(input: ApartmentInput): Promise<ActionResult<{ id: string }>> {
  return run(async () => {
    const r = await m(api.apartments.create, clean(input));
    rev(r.id);
    return r;
  });
}

export async function updateApartment(id: string, input: ApartmentInput): Promise<ActionResult<undefined>> {
  return run(async () => {
    await m(api.apartments.update, { id, ...clean(input) });
    rev(id);
    return undefined;
  });
}

export async function setApartmentStatus(id: string, status: (typeof APARTMENT_STATUSES)[number], reason?: string): Promise<ActionResult<undefined>> {
  return run(async () => {
    await m(api.apartments.setStatus, { id, status, reason });
    rev(id);
    return undefined;
  });
}

export async function setCleaningStatus(id: string, cleaningStatus: (typeof CLEANING_STATUSES)[number], notes?: string): Promise<ActionResult<undefined>> {
  return run(async () => {
    await m(api.apartments.setCleaningStatus, { id, cleaningStatus, notes });
    rev(id);
    return undefined;
  });
}

export async function blockDates(input: { apartmentId: string; startDate: string; endDate: string; reason?: string }): Promise<ActionResult<undefined>> {
  return run(async () => {
    await m(api.apartments.blockDates, input);
    rev(input.apartmentId);
    return undefined;
  });
}

export async function unblockDates(blockId: string): Promise<ActionResult<undefined>> {
  return run(async () => {
    await m(api.inventoryOps.removeBlock, { blockId });
    rev();
    return undefined;
  });
}

export async function deleteApartment(id: string, reason: string): Promise<ActionResult<undefined>> {
  return run(async () => {
    await m(api.apartments.remove, { id, reason });
    rev();
    return undefined;
  });
}
