#!/usr/bin/env node
/**
 * Generates Sports Center feature pages and thin app routes from registry.
 * Run: node scripts/generate-sports-center-routes.mjs
 */
import fs from "node:fs";
import path from "node:path";

const ROOT = path.resolve(import.meta.dirname, "..");
const FEATURES_DIR = path.join(ROOT, "modules/sports-center/features");
const APP_DIR = path.join(ROOT, "app/sports-center");

/** @type {Array<{file:string, exportName:string, route:string, config:object}>} */
const CRUD_PAGES = [
  { file: "sportsCenters", exportName: "SportsCentersPage", route: "sports-centers", config: { title: "مراکز ورزشی", resourceKey: "sportsCenters", apiKey: "sportsCenters", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }, { key: "status", header: "وضعیت", type: "status" }], createFields: [{ name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "branches", exportName: "BranchesPage", route: "branches", config: { title: "شعب", resourceKey: "branches", apiKey: "branches", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }, { key: "status", header: "وضعیت", type: "status" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "sports", exportName: "SportsPage", route: "sports", config: { title: "رشته‌های ورزشی", resourceKey: "sports", apiKey: "sports", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }, { key: "category", header: "دسته" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "members", exportName: "MembersPage", route: "members", config: { title: "اعضا", resourceKey: "members", apiKey: "members", columns: [{ key: "member_number", header: "شماره عضو" }, { key: "display_name", header: "نام" }, { key: "mobile", header: "موبایل" }, { key: "status", header: "وضعیت", type: "status" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "display_name", label: "نام نمایشی", required: true }, { name: "mobile", label: "موبایل" }] } },
  { file: "families", exportName: "FamiliesPage", route: "families", config: { title: "خانواده‌ها", resourceKey: "familyMembers", apiKey: "familyMembers", listQuery: "member_id", columns: [{ key: "display_name", header: "نام" }, { key: "relationship", header: "نسبت" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "member_id", label: "شناسه عضو", required: true }, { name: "display_name", label: "نام", required: true }], readOnly: true } },
  { file: "medicalRecords", exportName: "MedicalRecordsPage", route: "medical-records", config: { title: "سوابق پزشکی", resourceKey: "medicalInformation", apiKey: "medicalInformation", createOnly: true, columns: [{ key: "member_id", header: "عضو" }, { key: "updated_at", header: "به‌روزرسانی", type: "datetime" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "member_id", label: "شناسه عضو", required: true }] } },
  { file: "memberships", exportName: "MembershipsPage", route: "memberships", config: { title: "عضویت‌ها", resourceKey: "memberships", apiKey: "memberships", columns: [{ key: "membership_number", header: "شماره" }, { key: "status", header: "وضعیت", type: "status" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "membership_type_id", label: "نوع عضویت", required: true }] } },
  { file: "membershipTypes", exportName: "MembershipTypesPage", route: "membership-types", config: { title: "انواع عضویت", resourceKey: "membershipTypes", apiKey: "membershipTypes", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }, { key: "status", header: "وضعیت", type: "status" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "sportCategories", exportName: "SportCategoriesPage", route: "catalog/sport-categories", config: { title: "دسته‌بندی رشته‌ها", resourceKey: "sportCategories", apiKey: "sportCategories", feature: "membership_catalog", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "ageGroups", exportName: "AgeGroupsPage", route: "catalog/age-groups", config: { title: "گروه‌های سنی", resourceKey: "ageGroups", apiKey: "ageGroups", feature: "membership_catalog", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "pricingModels", exportName: "PricingModelsPage", route: "catalog/pricing-models", config: { title: "مدل‌های قیمت‌گذاری", resourceKey: "pricingModels", apiKey: "pricingModels", feature: "membership_catalog", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "membershipPackages", exportName: "MembershipPackagesPage", route: "catalog/packages", config: { title: "پکیج‌های عضویت", resourceKey: "membershipPackages", apiKey: "membershipPackages", feature: "membership_catalog", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "membershipPlans", exportName: "MembershipPlansPage", route: "catalog/plans", config: { title: "طرح‌های عضویت", resourceKey: "membershipPlans", apiKey: "membershipPlans", feature: "membership_catalog", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "membershipRules", exportName: "MembershipRulesPage", route: "catalog/rules", config: { title: "قوانین عضویت", resourceKey: "membershipRules", apiKey: "membershipRules", feature: "membership_catalog", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "renewalPolicies", exportName: "RenewalPoliciesPage", route: "catalog/renewal-policies", config: { title: "سیاست تمدید", resourceKey: "renewalPolicies", apiKey: "renewalPolicies", feature: "membership_catalog", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "freezingRules", exportName: "FreezingRulesPage", route: "catalog/freezing-rules", config: { title: "قوانین فریز", resourceKey: "freezingRules", apiKey: "freezingRules", feature: "membership_catalog", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "expirationPolicies", exportName: "ExpirationPoliciesPage", route: "catalog/expiration-policies", config: { title: "سیاست انقضا", resourceKey: "expirationPolicies", apiKey: "expirationPolicies", feature: "membership_catalog", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "coaches", exportName: "CoachesPage", route: "coaches", config: { title: "مربیان", resourceKey: "coaches", apiKey: "coaches", columns: [{ key: "code", header: "کد" }, { key: "display_name", header: "نام" }, { key: "staff_kind", header: "نقش" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "display_name", label: "نام", required: true }] } },
  { file: "staffCertificates", exportName: "StaffCertificatesPage", route: "staff/certificates", config: { title: "گواهینامه‌ها", resourceKey: "staffCertificates", apiKey: "staffCertificates", feature: "coach_staff_management", listQuery: "coach_id", createOnly: true, columns: [{ key: "title", header: "عنوان" }, { key: "coach_id", header: "مربی" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "coach_id", label: "شناسه مربی", required: true }, { name: "title", label: "عنوان", required: true }] } },
  { file: "staffSkills", exportName: "StaffSkillsPage", route: "staff/skills", config: { title: "مهارت‌ها", resourceKey: "staffSkills", apiKey: "staffSkills", feature: "coach_staff_management", listQuery: "coach_id", createOnly: true, columns: [{ key: "skill_name", header: "مهارت" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "coach_id", label: "شناسه مربی", required: true }, { name: "skill_name", label: "مهارت", required: true }] } },
  { file: "staffWorkingHours", exportName: "StaffWorkingHoursPage", route: "staff/working-hours", config: { title: "ساعات کاری", resourceKey: "staffWorkingHours", apiKey: "staffWorkingHours", feature: "coach_staff_management", listQuery: "coach_id", createOnly: true, columns: [{ key: "day_of_week", header: "روز" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "coach_id", label: "شناسه مربی", required: true }] } },
  { file: "staffAvailability", exportName: "StaffAvailabilityPage", route: "staff/availability", config: { title: "دسترسی", resourceKey: "staffAvailability", apiKey: "staffAvailability", feature: "coach_staff_management", listQuery: "coach_id", createOnly: true, columns: [{ key: "status", header: "وضعیت", type: "status" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "coach_id", label: "شناسه مربی", required: true }] } },
  { file: "staffAssignments", exportName: "StaffAssignmentsPage", route: "staff/assignments", config: { title: "انتساب‌ها", resourceKey: "staffAssignments", apiKey: "staffAssignments", feature: "coach_staff_management", listQuery: "coach_id", createOnly: true, columns: [{ key: "assignment_kind", header: "نوع" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "coach_id", label: "شناسه مربی", required: true }] } },
  { file: "facilities", exportName: "FacilitiesPage", route: "facilities", config: { title: "امکانات", resourceKey: "facilities", apiKey: "facilities", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "courts", exportName: "CourtsPage", route: "courts", config: { title: "زمین‌ها", resourceKey: "courts", apiKey: "courts", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "rooms", exportName: "RoomsPage", route: "rooms", config: { title: "اتاق‌ها", resourceKey: "rooms", apiKey: "rooms", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "lockerRooms", exportName: "LockerRoomsPage", route: "locker-rooms", config: { title: "رختکن‌ها", resourceKey: "lockerRooms", apiKey: "lockerRooms", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "lockers", exportName: "LockersPage", route: "lockers", config: { title: "کمدها", resourceKey: "lockers", apiKey: "lockers", columns: [{ key: "code", header: "کد" }, { key: "status", header: "وضعیت", type: "status" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "locker_room_id", label: "رختکن", required: true }, { name: "code", label: "کد", required: true }] } },
  { file: "equipment", exportName: "EquipmentPage", route: "equipment", config: { title: "تجهیزات", resourceKey: "equipment", apiKey: "equipment", feature: "booking_engine", createOnly: true, columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "sessions", exportName: "SessionsPage", route: "sessions", config: { title: "جلسات", resourceKey: "sessions", apiKey: "sessions", feature: "booking_engine", listQuery: "sports_center_id", createOnly: true, columns: [{ key: "title", header: "عنوان" }, { key: "status", header: "وضعیت", type: "status" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "title", label: "عنوان", required: true }] } },
  { file: "bookings", exportName: "BookingsPage", route: "bookings", config: { title: "رزروها", resourceKey: "bookings", apiKey: "bookings", feature: "booking_engine", createOnly: true, columns: [{ key: "status", header: "وضعیت", type: "status" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "session_id", label: "جلسه", required: true }, { name: "member_id", label: "عضو", required: true }] } },
  { file: "attendance", exportName: "AttendancePage", route: "attendance", config: { title: "حضور و غیاب", resourceKey: "attendanceRecords", apiKey: "attendanceRecords", feature: "attendance_engine", listQuery: "member_id", createOnly: true, columns: [{ key: "kind", header: "نوع" }, { key: "recorded_at", header: "زمان", type: "datetime" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "member_id", label: "عضو", required: true }] } },
  { file: "accessLogs", exportName: "AccessLogsPage", route: "access-logs", config: { title: "لاگ دسترسی", resourceKey: "accessLogs", apiKey: "accessLogs", feature: "attendance_engine", listQuery: "member_id", readOnly: true, columns: [{ key: "access_result", header: "نتیجه" }, { key: "recorded_at", header: "زمان", type: "datetime" }] } },
  { file: "trainingPrograms", exportName: "TrainingProgramsPage", route: "training/programs", config: { title: "برنامه‌های تمرینی", resourceKey: "trainingPrograms", apiKey: "trainingPrograms", feature: "training_management", createOnly: true, columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "exercises", exportName: "ExercisesPage", route: "training/exercises", config: { title: "تمرینات", resourceKey: "exercises", apiKey: "exercises", feature: "training_management", createOnly: true, columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "workoutPlans", exportName: "WorkoutPlansPage", route: "training/workout-plans", config: { title: "برنامه تمرین", resourceKey: "workoutPlans", apiKey: "workoutPlans", feature: "training_management", createOnly: true, columns: [{ key: "title", header: "عنوان" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "title", label: "عنوان", required: true }] } },
  { file: "workoutAssignments", exportName: "WorkoutAssignmentsPage", route: "training/workout-assignments", config: { title: "انتساب تمرین", resourceKey: "workoutAssignments", apiKey: "workoutAssignments", feature: "training_management", createOnly: true, columns: [{ key: "member_id", header: "عضو" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "workout_plan_id", label: "برنامه", required: true }, { name: "member_id", label: "عضو", required: true }] } },
  { file: "measurements", exportName: "MeasurementsPage", route: "training/measurements", config: { title: "اندازه‌گیری‌ها", resourceKey: "measurements", apiKey: "measurements", feature: "training_management", createOnly: true, columns: [{ key: "member_id", header: "عضو" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "member_id", label: "عضو", required: true }] } },
  { file: "progress", exportName: "ProgressPage", route: "training/progress", config: { title: "پیشرفت", resourceKey: "progressRecords", apiKey: "progressRecords", feature: "training_management", createOnly: true, columns: [{ key: "member_id", header: "عضو" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "member_id", label: "عضو", required: true }] } },
  { file: "nutrition", exportName: "NutritionPage", route: "training/nutrition", config: { title: "تغذیه", resourceKey: "nutritionPlans", apiKey: "nutritionPlans", feature: "training_management", createOnly: true, columns: [{ key: "title", header: "عنوان" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "member_id", label: "عضو", required: true }, { name: "title", label: "عنوان", required: true }] } },
  { file: "competitions", exportName: "CompetitionsPage", route: "competitions", config: { title: "مسابقات", resourceKey: "competitions", apiKey: "competitions", feature: "competition_management", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }, { key: "status", header: "وضعیت", type: "status" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "teams", exportName: "TeamsPage", route: "competitions/teams", config: { title: "تیم‌ها", resourceKey: "competitionTeams", apiKey: "competitionTeams", feature: "competition_management", listQuery: "competition_id", createOnly: true, columns: [{ key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "competition_id", label: "مسابقه", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "competitionResults", exportName: "CompetitionResultsPage", route: "competitions/results", config: { title: "نتایج", resourceKey: "competitionResults", apiKey: "competitionResults", feature: "competition_management", createOnly: true, columns: [{ key: "competition_id", header: "مسابقه" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "competition_id", label: "مسابقه", required: true }] } },
  { file: "devices", exportName: "DevicesPage", route: "devices", config: { title: "دستگاه‌ها", resourceKey: "devices", apiKey: "devices", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }, { key: "status", header: "وضعیت", type: "status" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "deviceProviders", exportName: "DeviceProvidersPage", route: "device-providers", config: { title: "ارائه‌دهندگان دستگاه", resourceKey: "deviceProviders", apiKey: "deviceProviders", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "attendanceGateways", exportName: "AttendanceGatewaysPage", route: "attendance-gateways", config: { title: "گیت‌وی حضور", resourceKey: "attendanceGateways", apiKey: "attendanceGateways", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "roles", exportName: "RolesPage", route: "roles", config: { title: "نقش‌ها", resourceKey: "roles", apiKey: "roles", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "permissions", exportName: "PermissionsPage", route: "permissions", config: { title: "مجوزها", resourceKey: "permissions", apiKey: "permissions", columns: [{ key: "code", header: "کد" }, { key: "name", header: "نام" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "name", label: "نام", required: true }] } },
  { file: "settings", exportName: "SettingsPage", route: "settings", config: { title: "تنظیمات", resourceKey: "settings", apiKey: "settings", createOnly: true, columns: [{ key: "key", header: "کلید" }, { key: "value", header: "مقدار" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "key", label: "کلید", required: true }, { name: "value", label: "مقدار", required: true }] } },
  { file: "events", exportName: "EventsPage", route: "events", config: { title: "رویدادها", resourceKey: "events", apiKey: "events", columns: [{ key: "code", header: "کد" }, { key: "title", header: "عنوان" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "code", label: "کد", required: true }, { name: "title", label: "عنوان", required: true }] } },
  { file: "membershipCards", exportName: "MembershipCardsPage", route: "membership-cards", config: { title: "کارت عضویت", resourceKey: "membershipCards", apiKey: "membershipCards", listQuery: "member_id", createOnly: true, columns: [{ key: "card_number", header: "شماره کارت" }, { key: "status", header: "وضعیت", type: "status" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "member_id", label: "عضو", required: true }] } },
  { file: "digitalMembership", exportName: "DigitalMembershipPage", route: "digital-membership", config: { title: "عضویت دیجیتال", resourceKey: "digitalMemberships", apiKey: "digitalMemberships", listQuery: "member_id", createOnly: true, columns: [{ key: "member_id", header: "عضو" }, { key: "status", header: "وضعیت", type: "status" }], createFields: [{ name: "sports_center_id", label: "شناسه مرکز", required: true }, { name: "member_id", label: "عضو", required: true }] } },
];

const SPECIAL_PAGES = [
  { file: "dashboard", exportName: "OwnerDashboard", route: "owner", feature: "dashboard.tsx" },
  { file: "health", exportName: "HealthPage", route: "health", feature: "health.tsx" },
  { file: "capabilities", exportName: "CapabilitiesPage", route: "capabilities", feature: "capabilities.tsx" },
  { file: "hub", exportName: "SportsCenterHub", route: "hub", feature: "hub.tsx" },
];

function featureContent(page) {
  const c = page.config;
  const featureLine = c.feature ? `\n  feature: "${c.feature}",` : "";
  const readOnly = c.readOnly ? "\n  readOnly: true," : "";
  const createOnly = c.createOnly ? "\n  createOnly: true," : "";
  const listQuery = c.listQuery ? `\n  listQueryParam: "${c.listQuery}",` : "";
  return `"use client";

import { createSportsCenterListPage } from "@/modules/sports-center/components/SportsCenterListCrudPage";
import { sportsCenterApi } from "@/modules/sports-center/services/sports-center-api";

export const ${page.exportName} = createSportsCenterListPage({
  title: ${JSON.stringify(c.title)},
  breadcrumb: ${JSON.stringify(c.title)},
  resourceKey: ${JSON.stringify(c.resourceKey)},
  api: sportsCenterApi.${c.apiKey},${featureLine}${readOnly}${createOnly}${listQuery}
  columns: ${JSON.stringify(c.columns, null, 2).replace(/\n/g, "\n  ")},
  createFields: ${JSON.stringify(c.createFields ?? [], null, 2).replace(/\n/g, "\n  ")},
});

export default ${page.exportName};
`;
}

function writeAppRoute(route, exportName, file) {
  const dir = path.join(APP_DIR, route);
  fs.mkdirSync(dir, { recursive: true });
  fs.writeFileSync(
    path.join(dir, "page.tsx"),
    `"use client";\n\nexport { ${exportName} as default } from "@/modules/sports-center/features/${file}";\n`
  );
  fs.writeFileSync(
    path.join(dir, "loading.tsx"),
    `import { LoadingState } from "@/components/ds";\n\nexport default function Loading() {\n  return <LoadingState label="در حال بارگذاری…" />;\n}\n`
  );
  fs.writeFileSync(
    path.join(dir, "error.tsx"),
    `"use client";\n\nimport { ErrorState } from "@/components/ds";\n\nexport default function Error({ error, reset }: { error: Error; reset: () => void }) {\n  return <ErrorState message={error.message} onRetry={reset} />;\n}\n`
  );
}

fs.mkdirSync(FEATURES_DIR, { recursive: true });

for (const page of CRUD_PAGES) {
  const featurePath = path.join(FEATURES_DIR, `${page.file}.tsx`);
  fs.writeFileSync(featurePath, featureContent(page));
  writeAppRoute(page.route, page.exportName, page.file);
  console.log("Generated", page.route);
}

console.log(`Done. ${CRUD_PAGES.length} CRUD pages generated.`);
