/**
 * @ai-summary Type definitions for ownership-costs feature
 * @ai-context Tracks vehicle ownership costs (insurance, registration, tax, inspection, parking, other)
 */

import { z } from 'zod';

// Ownership cost types
export type OwnershipCostType = 'insurance' | 'registration' | 'tax' | 'inspection' | 'parking' | 'other';

// Database record type (camelCase for TypeScript)
export interface OwnershipCost {
  id: string;
  userId: string;
  vehicleId: string;
  documentId?: string;
  costType: OwnershipCostType;
  amount: number;
  description?: string;
  periodStart?: string;
  periodEnd?: string;
  notes?: string;
  createdAt: string;
  updatedAt: string;
}

// Zod schemas for validation (camelCase for API)
export const OwnershipCostTypeSchema = z.enum(['insurance', 'registration', 'tax', 'inspection', 'parking', 'other']);

export const CreateOwnershipCostSchema = z.object({
  vehicleId: z.string().uuid(),
  documentId: z.string().uuid().optional(),
  costType: OwnershipCostTypeSchema,
  amount: z.number().positive(),
  description: z.string().max(200).optional(),
  periodStart: z.string().optional(),
  periodEnd: z.string().optional(),
  notes: z.string().max(10000).optional(),
});
export type CreateOwnershipCostRequest = z.infer<typeof CreateOwnershipCostSchema>;

export const UpdateOwnershipCostSchema = z.object({
  documentId: z.string().uuid().nullable().optional(),
  costType: OwnershipCostTypeSchema.optional(),
  amount: z.number().positive().optional(),
  description: z.string().max(200).nullable().optional(),
  periodStart: z.string().nullable().optional(),
  periodEnd: z.string().nullable().optional(),
  notes: z.string().max(10000).nullable().optional(),
});
export type UpdateOwnershipCostRequest = z.infer<typeof UpdateOwnershipCostSchema>;

// Response type (alias to OwnershipCost)
export type OwnershipCostResponse = OwnershipCost;

// TCO aggregation stats
// NOTE: Extended for backward compatibility with vehicles service
// The spec calls for totalCost and recordCount only, but the vehicles service
// expects breakdown by cost type. This will be cleaned up when vehicles service
// is refactored to use the new ownership-costs table directly.
export interface OwnershipCostStats {
  totalCost: number;
  recordCount: number;
  // Backward compatibility fields for vehicles service
  insuranceCosts?: number;
  registrationCosts?: number;
  taxCosts?: number;
  otherCosts?: number;
}
