import { z } from "zod";

export type SessionPayload = {
  userId: string;
  email: string;
  expiresAt: Date | string;
};

export type ActionState = {
  success?: boolean;
  message?: string;
  errors?: Record<string, string[] | undefined>;
};

export const LoginSchema = z.object({
  email: z.email({ error: "Please enter a valid email." }).trim(),
  password: z.string().min(1, { error: "Password is required." }),
});

export const ContactSchema = z.object({
  name: z.string().trim().min(2, { error: "Name is required." }).max(120),
  email: z.email({ error: "Please enter a valid email." }).trim(),
  phone: z.string().trim().max(40).optional().or(z.literal("")),
  subject: z.string().trim().min(1, { error: "Subject is required." }).max(120),
  message: z
    .string()
    .trim()
    .min(10, { error: "Message must be at least 10 characters." })
    .max(5000),
  website: z.string().max(0).optional().or(z.literal("")),
});

export const AdmissionSchema = z.object({
  studentFirstName: z
    .string()
    .trim()
    .min(1, { error: "First name is required." })
    .max(80),
  studentLastName: z
    .string()
    .trim()
    .min(1, { error: "Last name is required." })
    .max(80),
  studentDob: z.string().trim().min(1, { error: "Date of birth is required." }),
  studentGender: z.string().trim().max(40).optional().or(z.literal("")),
  currentSchool: z.string().trim().max(160).optional().or(z.literal("")),
  gradeLevel: z.string().trim().min(1, { error: "Grade is required." }),
  program: z.string().trim().min(1, { error: "Program is required." }),
  startDate: z.string().trim().min(1, { error: "Start date is required." }),
  parentName: z
    .string()
    .trim()
    .min(1, { error: "Parent / guardian name is required." })
    .max(120),
  relationship: z
    .string()
    .trim()
    .min(1, { error: "Relationship is required." }),
  email: z.email({ error: "Please enter a valid email." }).trim(),
  phone: z.string().trim().min(7, { error: "Phone is required." }).max(40),
  notes: z.string().trim().max(5000).optional().or(z.literal("")),
  consent: z.literal(true, {
    error: "Please confirm the information and consent to proceed.",
  }),
  website: z.string().max(0).optional().or(z.literal("")),
});

export const ProgramSchema = z.object({
  title: z.string().trim().min(2, { error: "Title is required." }).max(160),
  description: z
    .string()
    .trim()
    .min(10, { error: "Description is required." })
    .max(2000),
  sortOrder: z.coerce.number().int().min(0).default(0),
  published: z.coerce.boolean().default(true),
});

export const BlogPostSchema = z.object({
  title: z.string().trim().min(2, { error: "Title is required." }).max(200),
  category: z
    .string()
    .trim()
    .min(2, { error: "Category is required." })
    .max(80),
  excerpt: z
    .string()
    .trim()
    .min(10, { error: "Excerpt is required." })
    .max(1000),
  imageUrl: z.string().trim().max(300).optional().or(z.literal("")),
  featured: z.coerce.boolean().default(false),
  sortOrder: z.coerce.number().int().min(0).default(0),
  published: z.coerce.boolean().default(true),
});

export const SubmissionStatusSchema = z.enum([
  "new",
  "in_progress",
  "resolved",
]);

export const HomepageItemSchema = z.object({
  label: z.string().trim().min(1, { error: "Label is required." }).max(160),
  text: z.string().trim().max(1000).optional().or(z.literal("")),
  href: z.string().trim().max(300).optional().or(z.literal("")),
  altText: z.string().trim().max(200).optional().or(z.literal("")),
  sortOrder: z.coerce.number().int().min(0).default(0),
  published: z.coerce.boolean().default(true),
});

export const HeaderSettingsSchema = z.object({
  header_title: z.string().trim().min(1).max(80),
  header_subtitle: z.string().trim().min(1).max(80),
});

export const HeroSettingsSchema = z.object({
  hero_heading: z.string().trim().min(2).max(200),
  hero_description: z.string().trim().min(2).max(500),
  hero_cta_text: z.string().trim().min(1).max(60),
  hero_cta_href: z.string().trim().min(1).max(200),
});

export const GedSettingsSchema = z.object({
  ged_heading: z.string().trim().min(2).max(200),
  ged_body: z.string().trim().min(2).max(1000),
  ged_seal_top: z.string().trim().min(1).max(40),
  ged_seal_main: z.string().trim().min(1).max(40),
  ged_seal_bottom: z.string().trim().min(1).max(60),
});

export const AboutSettingsSchema = z.object({
  about_heading: z.string().trim().min(2).max(160),
  about_body: z.string().trim().min(2).max(1000),
  about_cta_text: z.string().trim().min(1).max(60),
  about_cta_href: z.string().trim().min(1).max(200),
});

export const ProgramsSectionSettingsSchema = z.object({
  programs_heading: z.string().trim().min(2).max(160),
  programs_body: z.string().trim().min(2).max(500),
});

export const PartnersSectionSettingsSchema = z.object({
  partners_label: z.string().trim().min(1).max(80),
});

export const BlogsSectionSettingsSchema = z.object({
  blogs_heading: z.string().trim().min(2).max(160),
  blogs_body: z.string().trim().min(2).max(500),
});

export const FooterSettingsSchema = z.object({
  footer_title: z.string().trim().min(2).max(160),
  footer_description: z.string().trim().min(2).max(500),
  footer_address: z.string().trim().min(2).max(300),
  footer_phone: z.string().trim().min(5).max(40),
  footer_email: z.string().trim().min(3).max(120),
  footer_programs_heading: z.string().trim().min(1).max(80),
  footer_contact_heading: z.string().trim().min(1).max(80),
});

export const HERO_SETTING_KEYS = [
  "hero_heading",
  "hero_description",
  "hero_cta_text",
  "hero_cta_href",
] as const;

export const HOMEPAGE_SECTION_KEYS = [
  "hero",
  "ged",
  "about",
  "programs",
  "partners",
  "blogs",
] as const;

export type HomepageSectionKey = (typeof HOMEPAGE_SECTION_KEYS)[number];
