import type { Refund } from '$lib/types/index';

export interface Service {
	id: string;
	name: string;
	description: string;
	price: number;
	duration_minutes: number;
	patch_test_duration_hours: number;
	minimum_age_required: number;
	patch_test_status?: 'ok' | 'required' | 'expired';
	is_custom?: boolean;
}

export interface CustomService extends Service {
	usage_count: number;
	last_used_at: string | null;
	notes: string | null;
	created_at: string;
	created_by?: string;
}

export interface CustomerInfo {
	firstName: string;
	lastName: string;
	email: string;
	phone: string;
	specialRequests: string;
}

export interface TimeSlot {
	time: string;
	available: boolean;
}

export interface WorkingHoursDay {
	date: string;
	weekday: number;
	startTime: string;
	endTime: string;
	isOpen: boolean;
	source: string;
}

export interface AvailableHoursDay {
	date: string;
	weekday: number;
	isOpen: boolean;
	slots: Array<{ startTime: string; endTime: string }>;
	source: string;
}

export type BookingStatus =
	| 'pending'
	| 'confirmed'
	| 'in_progress'
	| 'completed'
	| 'client_cancelled'
	| 'we_cancelled'
	| 'no_show'
	| 'pending_release'
	| 'deposit_lapsed';

export interface BookingService {
	booking_id: string;
	service_id: string;
	override_price?: number;
	override_duration_minutes?: number;
	service_name?: string;
	service_description?: string;
	price?: number;
	duration_minutes?: number;
}

export interface BookingUser {
	id: string;
	first_name: string;
	last_name: string;
	full_name: string;
	email?: string;
	phone?: string;
	profile_pic_url?: string;
	date_of_birth?: string;
	account_role: string;
	loyalty_stamps?: number;
	referral_code?: string;
	referral_code_uses?: number;
	created_at: string;
	notes?: string;
	previous_first_name?: string;
	previous_last_name?: string;
}

export interface Payment {
	id: string;
	booking_id: string;
	payment_type: 'deposit' | 'full' | 'tip' | 'balance' | 'partial';
	payment_method: 'online_square' | 'in_person_card' | 'cash' | 'giftcard' | 'discount';
	vendor_code?: string;
	invoice_number?: number;
	status: 'pending' | 'completed' | 'failed' | 'refunded';
	amount: number;
	card_last4?: string;
	is_vat_applicable: boolean;
	vat_rate?: number;
	vat_amount?: number;
	net_amount?: number;
	created_at: string;
	updated_at: string;
	created_by?: string;
}

export interface Booking {
	id: string;
	start_time: string;
	status: BookingStatus;
	notes?: string;
	created_at: string;
	updated_at: string;
	created_by?: string;
	created_by_name?: string;
	out_of_hours?: boolean;

	// Deposit fields
	deposit_required: boolean;
	deposit_amount?: number;
	deposit_paid: boolean;
	deposit_deadline?: string;

	user_id?: string;

	// Joined fields
	user?: BookingUser;
	services?: BookingService[];
	payments?: Payment[];
	refunds?: Refund[];
	discounts?: BookingDiscount[];
	total_amount: number;
	amount_paid: number;
	amount_due: number;
	duration_minutes: number;
}

export interface LoyaltyRedemption {
	id: string;
	user_id: string;
	stamps_redeemed: number;
	status: 'pending' | 'applied' | 'expired';
	applied_to_booking_id?: string;
	redeemed_at: string;
	applied_at?: string;
	expires_at: string;
}

export type CampaignType = 'time_based' | 'milestone';
export type MilestoneType = 'per_user_booking_count' | 'global_booking_count' | 'anniversary';
export type MilestoneUnit = 'bookings' | 'months' | 'years';
export type DiscountCampaignScope = 'all_bookings' | 'first_booking_only' | 'new_customers_only';
export type DiscountCampaignStatus = 'draft' | 'active' | 'completed' | 'cancelled';

export interface DiscountCampaign {
	id: string;
	name: string;
	description?: string;
	campaign_type: CampaignType;
	discount_percent: number;
	scope?: DiscountCampaignScope;
	start_date?: string;
	end_date?: string;
	milestone_type?: MilestoneType;
	milestone_value?: number;
	milestone_unit?: MilestoneUnit;
	status: DiscountCampaignStatus;
	max_redemptions?: number;
	times_redeemed: number;
	created_at: string;
	updated_at: string;
	created_by?: string;
}

export interface BookingDiscount {
	id: string;
	booking_id: string;
	user_id: string;
	discount_source: 'loyalty' | 'campaign' | 'referral';
	source_id?: string;
	campaign_name?: string;
	campaign_type?: CampaignType;
	milestone_type?: MilestoneType;
	discount_percent: number;
	original_total: number;
	discount_amount: number;
	applied_at: string;
}

export interface CampaignStats {
	campaign: DiscountCampaign;
	total_discount_amount: number;
	booking_count: number;
}
