import {
  addProjectSchema,
  addUnitSchema,
  paymentTermSchema,
  uploadTimelineFileSchema,
  uploadFileSchema,
  uploadTimelineFile,
  photoUploadSchema,
} from "@/schema/projectSchema";

export type AddProjectFormValues = z.infer<typeof addProjectSchema>;

export type AddUnitFormValues = z.infer<typeof addUnitSchema>;

export type AddFileFormValues = z.infer<typeof uploadFileSchema>;

export type AddPaymentFormValues = z.infer<typeof paymentTermSchema>;

export type ProgressTimeLineValues = z.infer<typeof uploadTimelineFileSchema>;

export type TimelineFileValues = z.infer<typeof photoUploadSchema>;

type apiFields = {
  _id: string;
  name: string;
};

export interface DetailsProjectProps {
  project: Project;
}

export type DetailsHeaderProps = {
  project: Pick<Project, "name" | "type" | "status" | "location">;
};

export interface Unit {
  id: string;
  unitNumber: string;
  block?: string;
  floor?: number | null;
  propertyType: {
    name: string;
    id: string;
  };
  size: number;
  status: string;
  price: number;
  soldAt: number | null;
  soldRate?: number | null;
  customerName?: string | null;
  soldBy?: string | null;
  salesDate?: string | null;
  paymentReceived: number;
  paymentDue: number;
}

export interface FileRecord {
  id: number | string;
  name: string;
  type: string;
  uploadedBy: string;
  uploadDate: string;
  size: string;
  fileType: string;
}

export interface PaymentTerm {
  id: string;
  name: string;
  stage: string;
  triggerType: string;
  triggerValue: string;
  documentTemplateId: string;
  documentTemplate: string;
  paymentPlans: string[];
  isActive: boolean;
  sendEmail: boolean;
  sendSMS: boolean;
}

export interface ProgressTimeLine {
  id: string;
  name: string;
  label: string;
  startDate: string;
  endDate: string;
  hasDependencies: boolean;
  dependencies: string[];
  status: string;
  progress: number;
}

export type PhotoItem = {
  photoUrl: string;
  caption?: string;
  file?: File;
};

export interface TimelineFile {
  photos: PhotoItem[];
}

// Common base types
export interface BaseEntity {
  id: string;
  createdAt?: string | Date;
  updatedAt?: string | Date;
}

export interface ApiFields {
  id: string;
  _id?: string;
  name: string;
}

export interface UserFields {
  _id: string;
  firstName: string;
  lastName: string;
  email: string;
}

export interface BaseResponse<T = any> {
  data: T;
  code: number;
  message: string;
  success: boolean;
  error?: object;
}

export interface PaginatedResponse<T> {
  results: T[];
  page: number;
  limit: number;
  totalPages: number;
  totalResults: number;
}

// Common query parameters
export interface BaseQueryParams {
  page?: number;
  limit?: number;
  search?: string;
  addedBy?: string;
  populate?: string;
  includeTimeStamps?: boolean;
  companyId?: string;
}

export interface FilterableQueryParams extends BaseQueryParams {
  propertyType?: string;
  creationMethod?: string;
}

// Project interfaces
export interface Project extends BaseEntity {
  projectName: string;
  normalizedName: string;
  description?: string;
  isSystemGenerated: boolean;
  searchCount: number;
  propertyType: ApiFields;
  subCategory: ApiFields;
  locality: ApiFields;
  city?: ApiFields;
  state?: ApiFields;
  status: string;
  pincode?: number;
  possessionDate: string;
  pricePerSqYard?: number;
  landmark?: string;
  reraId?: string;
  isDraft: boolean;
  creationMethod: string;
  hasBlockLayout?: boolean;
  createdBy: UserFields;
  updatedBy?: UserFields;
  units: Unit[];
  timelines: ProgressTimeLine[];
  paymentTerms: PaymentTerm[];
  files: FileRecord[];
  amenities: {
    id: string;
    name: string;
  }[];
  mediaUrls: string[];
  visibleToUsers?: Array<string | { id?: string; _id?: string }>;
  visibleToTeams?: Array<string | { id?: string; _id?: string }>;
  isPublicVisibility?: boolean;
}

export interface CreateProjectBody {
  name: string;
  description?: string;
  propertyType?: string;
  locality?: string;
  city?: string;
  state?: string;
  status?: string;
  pincode?: number;
  possessionDate: string;
  pricePerSqYard?: number;
  landmark?: string;
  reraId?: string;
  isDraft?: boolean;
  creationMethod?: string;
  hasBlockLayout?: boolean;
}

export type UpdateProjectBody = Partial<CreateProjectBody>;

export interface UpdateProjectAmenitiesBody {
  amenities: string[];
}

export interface GetProjectsParams extends FilterableQueryParams {
  isViewOnly?: boolean;
}

export type GetProjectListResponse = BaseResponse<PaginatedResponse<Project>>;
export type GetProjectByIdResponse = BaseResponse<Project>;

export interface Unit extends BaseEntity {
  unitNumber: string;
  price?: number;
  status?: "available" | "sold" | "hold" | string;

  project?: string;
  propertyType?: {
    name: string;
    id: string;
  };
  size?: number;

  unitName?: string;
  projectId?: string;
  floorNumber?: number;
  blockNumber?: string;
  area?: number;
  areaUnit?: string;
  description?: string;
  features?: string[];

  createdBy?: UserFields | string;
  updatedBy?: UserFields | string;
}

export interface CreateUnitBody {
  unitName: string;
  unitNumber: string;
  floorNumber?: number;
  blockNumber?: string;
  area?: number;
  areaUnit?: string;
  price?: number;
  status?: string;
  description?: string;
  features?: string[];
}

export type UpdateUnitBody = Partial<CreateUnitBody>;

export interface GetUnitsParams extends FilterableQueryParams {
  status?: string;
  projectId: string;
  statuses?: string;
}

export type GetUnitListResponse = BaseResponse<PaginatedResponse<Unit>>;
export type GetUnitByIdResponse = BaseResponse<Unit>;

export interface ProjectSearchItem {
  value: string;
  label: string;
  type?: string;
}

export interface ProjectSearchResponse {
  data: { data: ProjectSearchItem[] };
  total: number;
  query: string;
}

export interface CreateProjectFromSearchResponse {
  data?: ProjectSearchItem;
  message?: string;
}

export interface CreateProjectFromSearchErrorResponse {
  error: string;
  similar: ProjectSearchItem[];
  requiresConfirmation: boolean;
}

export interface SearchProjectsParams extends Record<string, unknown> {
  q: string;
  limit?: number;
}

export interface CreateProjectFromSearchParams extends Record<string, unknown> {
  searchQuery: string;
  forceCreate?: boolean;
  area?: string;
  city?: string;
  state?: string;
}

export interface UploadUnitsExcelRequest {
  projectId: string;
  propertyType: string;
  file: File;
}

export interface UploadUnitsExcelResponse {
  data: {
    jobId: string;
  };
  code: number;
  message: string;
  success: boolean;
  error?: object;
}

export interface GetUnitsAnalyticsResponse {
  code: number;
  message: string;
  data: {
    totalUnits: number;
    soldUnits: number;
    holdUnits: number;
    availableUnits: number;
    totalSalesValue: number;
    totalReceived: number;
    totalDue: number;
    receivedPercentage: number;
    duePercentage: number;
  };
  success: boolean;
}

export interface GetProjectsAnalyticsResponse {
  code: number;
  message: string;
  data: {
    totalProjects: number;
    totalUnits: number;
    availableUnits: number;
    soldUnits: number;
    statusCounts: {
      under_construction: number;
      possession_soon: number;
      ready_possession: number;
      launch: number;
    };
    totalActiveLeads: number;
    totalPaymentReceivable: number;
    totalPaymentReceived: number;
    totalProjectValue: number;
  };
  success: boolean;
}

