import { BaseMetadata, Override } from '@/types/api/api-general';
import { BaseArea } from '@/types/api/master-data/area';
import { BaseLocation } from '@/types/api/master-data/location';
import { BaseKandang } from '@/types/api/master-data/kandang';
import { BaseUser } from '@/types/api/master-data/user';

export type WarehouseType = 'AREA' | 'LOKASI' | 'KANDANG';

export type BaseWarehouse = {
  id: number;
  name: string;
};

export type BaseWarehouseArea = BaseWarehouse & {
  type: 'AREA';
  area: BaseArea;
};

export type BaseWarehouseLocation = BaseWarehouse & {
  type: 'LOKASI';
  area: BaseArea;
  location: Override<
    BaseLocation,
    {
      area: BaseArea | null;
    }
  >;
};

export type BaseWarehouseKandang = BaseWarehouse & {
  type: 'KANDANG';
  area: BaseArea;
  location: Override<
    BaseLocation,
    {
      area: BaseArea | null;
    }
  >;
  kandang: Override<
    BaseKandang,
    {
      location: BaseLocation | null;
      pic: BaseUser | null;
    }
  >;
};

export type Warehouse = BaseMetadata &
  (BaseWarehouseArea | BaseWarehouseLocation | BaseWarehouseKandang);

export type BaseCreateWarehousePayload = {
  name: string;
};

export type BaseCreateWarehouseAreaPayload = BaseCreateWarehousePayload & {
  type: 'AREA';
  area_id: number;
};

export type BaseCreateWarehouseLocationPayload = Override<
  BaseCreateWarehouseAreaPayload,
  {
    type: 'LOKASI';
    location_id: number;
  }
>;

export type BaseCreateWarehouseKandangPayload = Override<
  BaseCreateWarehouseLocationPayload,
  {
    type: 'KANDANG';
    kandang_id: number;
  }
>;

export type CreateWarehousePayload =
  | BaseCreateWarehouseAreaPayload
  | BaseCreateWarehouseLocationPayload
  | BaseCreateWarehouseKandangPayload;

export type UpdateWarehousePayload = CreateWarehousePayload;
