CREATE TABLE `attendance` (
	`id` text PRIMARY KEY NOT NULL,
	`user_id` text NOT NULL,
	`check_in_time` integer NOT NULL,
	`check_out_time` integer,
	`type` text DEFAULT 'gym' NOT NULL,
	`notes` text,
	`created_at` integer NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `attendance_user_id_idx` ON `attendance` (`user_id`);--> statement-breakpoint
CREATE INDEX `attendance_check_in_time_idx` ON `attendance` (`check_in_time`);--> statement-breakpoint
CREATE INDEX `attendance_user_check_in_idx` ON `attendance` (`user_id`,`check_in_time`);--> statement-breakpoint
CREATE TABLE `clients` (
	`id` text PRIMARY KEY NOT NULL,
	`user_id` text NOT NULL,
	`membership_type` text DEFAULT 'basic' NOT NULL,
	`membership_status` text DEFAULT 'active' NOT NULL,
	`join_date` integer NOT NULL,
	`last_visit` integer,
	`emergency_contact_name` text,
	`emergency_contact_phone` text,
	`emergency_contact_relationship` text,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `clients_user_id_unique` ON `clients` (`user_id`);--> statement-breakpoint
CREATE INDEX `clients_user_id_idx` ON `clients` (`user_id`);--> statement-breakpoint
CREATE INDEX `clients_membership_status_idx` ON `clients` (`membership_status`);--> statement-breakpoint
CREATE TABLE `fitness_goals` (
	`id` text PRIMARY KEY NOT NULL,
	`user_id` text NOT NULL,
	`fitness_profile_id` text,
	`goal_type` text NOT NULL,
	`title` text NOT NULL,
	`description` text,
	`target_value` real,
	`current_value` real,
	`unit` text,
	`start_date` integer NOT NULL,
	`target_date` integer,
	`completed_date` integer,
	`status` text DEFAULT 'active' NOT NULL,
	`progress` real DEFAULT 0,
	`priority` text DEFAULT 'medium',
	`notes` text,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`fitness_profile_id`) REFERENCES `fitness_profiles`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `fitness_goals_user_id_idx` ON `fitness_goals` (`user_id`);--> statement-breakpoint
CREATE INDEX `fitness_goals_status_idx` ON `fitness_goals` (`status`);--> statement-breakpoint
CREATE INDEX `fitness_goals_user_status_idx` ON `fitness_goals` (`user_id`,`status`);--> statement-breakpoint
CREATE TABLE `fitness_profiles` (
	`id` text PRIMARY KEY NOT NULL,
	`user_id` text NOT NULL,
	`height` real,
	`weight` real,
	`age` integer,
	`gender` text,
	`fitness_goals` text,
	`activity_level` text,
	`medical_conditions` text,
	`allergies` text,
	`injuries` text,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `fitness_profiles_user_id_unique` ON `fitness_profiles` (`user_id`);--> statement-breakpoint
CREATE INDEX `fitness_profiles_user_id_idx` ON `fitness_profiles` (`user_id`);--> statement-breakpoint
CREATE TABLE `gyms` (
	`id` text PRIMARY KEY NOT NULL,
	`name` text NOT NULL,
	`location` text,
	`status` text DEFAULT 'active' NOT NULL,
	`admin_user_id` text NOT NULL,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`admin_user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `gyms_admin_user_id_idx` ON `gyms` (`admin_user_id`);--> statement-breakpoint
CREATE INDEX `gyms_status_idx` ON `gyms` (`status`);--> statement-breakpoint
CREATE TABLE `notifications` (
	`id` text PRIMARY KEY NOT NULL,
	`user_id` text NOT NULL,
	`title` text NOT NULL,
	`message` text NOT NULL,
	`type` text NOT NULL,
	`read` integer DEFAULT false NOT NULL,
	`created_at` integer NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `notifications_user_id_idx` ON `notifications` (`user_id`);--> statement-breakpoint
CREATE INDEX `notifications_read_idx` ON `notifications` (`read`);--> statement-breakpoint
CREATE INDEX `notifications_user_read_idx` ON `notifications` (`user_id`,`read`);--> statement-breakpoint
CREATE TABLE `payments` (
	`id` text PRIMARY KEY NOT NULL,
	`client_id` text NOT NULL,
	`amount` real NOT NULL,
	`currency` text DEFAULT 'USD' NOT NULL,
	`status` text DEFAULT 'pending' NOT NULL,
	`payment_method` text NOT NULL,
	`due_date` integer NOT NULL,
	`paid_at` integer,
	`description` text NOT NULL,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `payments_client_id_idx` ON `payments` (`client_id`);--> statement-breakpoint
CREATE INDEX `payments_status_idx` ON `payments` (`status`);--> statement-breakpoint
CREATE INDEX `payments_due_date_idx` ON `payments` (`due_date`);--> statement-breakpoint
CREATE TABLE `recommendations` (
	`id` text PRIMARY KEY NOT NULL,
	`user_id` text NOT NULL,
	`fitness_profile_id` text NOT NULL,
	`recommendation_text` text NOT NULL,
	`activity_plan` text NOT NULL,
	`diet_plan` text NOT NULL,
	`status` text DEFAULT 'pending' NOT NULL,
	`generated_at` integer NOT NULL,
	`approved_at` integer,
	`approved_by` text,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`fitness_profile_id`) REFERENCES `fitness_profiles`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `recommendations_user_id_idx` ON `recommendations` (`user_id`);--> statement-breakpoint
CREATE INDEX `recommendations_status_idx` ON `recommendations` (`status`);--> statement-breakpoint
CREATE INDEX `recommendations_fitness_profile_id_idx` ON `recommendations` (`fitness_profile_id`);--> statement-breakpoint
CREATE TABLE `trainer_clients` (
	`id` text PRIMARY KEY NOT NULL,
	`trainer_user_id` text NOT NULL,
	`client_user_id` text NOT NULL,
	`gym_id` text NOT NULL,
	`created_at` integer NOT NULL,
	FOREIGN KEY (`trainer_user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`client_user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`gym_id`) REFERENCES `gyms`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `trainer_clients_trainer_id_idx` ON `trainer_clients` (`trainer_user_id`);--> statement-breakpoint
CREATE INDEX `trainer_clients_client_id_idx` ON `trainer_clients` (`client_user_id`);--> statement-breakpoint
CREATE INDEX `trainer_clients_gym_id_idx` ON `trainer_clients` (`gym_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `trainer_client_unique` ON `trainer_clients` (`trainer_user_id`,`client_user_id`);--> statement-breakpoint
CREATE TABLE `users` (
	`id` text PRIMARY KEY NOT NULL,
	`email` text NOT NULL,
	`first_name` text NOT NULL,
	`last_name` text NOT NULL,
	`password` text,
	`role` text DEFAULT 'client' NOT NULL,
	`phone` text,
	`gym_id` text,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `users_email_unique` ON `users` (`email`);--> statement-breakpoint
CREATE INDEX `users_email_idx` ON `users` (`email`);--> statement-breakpoint
CREATE INDEX `users_gym_id_idx` ON `users` (`gym_id`);--> statement-breakpoint
CREATE INDEX `users_role_idx` ON `users` (`role`);