import type {
  AdminServiceCreatePayload,
  AdminServiceUpdatePayload,
} from '../../../../api';
import type { ServiceItem } from '../../../../api/services/types.ts';

export type ServiceFormValues = {
  title: string;
  slug: string;
  description: string;
  priceFrom: string;
  imageUrl: string;
  status: string;
  categoryId: string;
};

export const defaultServiceFormValues: ServiceFormValues = {
  title: '',
  slug: '',
  description: '',
  priceFrom: '',
  imageUrl: '',
  status: 'PUBLISHED',
  categoryId: '',
};

export const createServiceFormValues = (service?: Partial<ServiceItem>): ServiceFormValues => ({
  title: service?.title ?? '',
  slug: service?.slug ?? '',
  description: service?.description ?? '',
  priceFrom:
    service?.priceFrom !== undefined && service?.priceFrom !== null ? String(service.priceFrom) : '',
  imageUrl: service?.imageUrl ?? '',
  status: service?.status ?? 'PUBLISHED',
  categoryId:
    service?.category?.id !== undefined && service?.category?.id !== null ? String(service.category.id) : '',
});

export const mapServiceFormToCreatePayload = (values: ServiceFormValues): AdminServiceCreatePayload => ({
  title: values.title.trim(),
  slug: values.slug.trim(),
  description: values.description.trim(),
  priceFrom: values.priceFrom.trim() || undefined,
  imageUrl: values.imageUrl.trim() || undefined,
  status: values.status.trim() || undefined,
  categoryId: values.categoryId ? Number(values.categoryId) : undefined,
});

export const mapServiceFormToUpdatePayload = (values: ServiceFormValues): AdminServiceUpdatePayload => ({
  title: values.title.trim(),
  slug: values.slug.trim(),
  description: values.description.trim(),
  priceFrom: values.priceFrom.trim() || undefined,
  imageUrl: values.imageUrl.trim() || undefined,
  status: values.status.trim() || undefined,
  categoryId: values.categoryId ? Number(values.categoryId) : undefined,
});
