import { z } from "zod";

// Shared schemas for reusability
const OptionSchema = z.object({
    id: z.string(),
    label: z.string().min(1, "Option label is required"),
});

export const ScreeningQuestionSchema = z.object({
    id: z.string().optional(),
    question_text: z.string().min(1, "Question text is required").max(500, "Question too long"),
    question_type: z.enum([
        "short_answer",
        "long_answer",
        "multiple_choice",
        "checkbox",
        "dropdown",
        "link",
        "file",
    ]),
    is_required: z.boolean().default(true),
    options: z.array(OptionSchema).optional(),
    ideal_answer: z.string().optional(),
    order: z.number().int().optional(),
});

// Main Multi-Step Form Schema
export const JobPostingSchema = z.object({
    // Step 1: Job Basics
    title: z
        .string()
        .min(10, "Job title is too short — please use at least 10 characters")
        .max(100, "Job title is too long — keep it under 100 characters"),
    company: z.string().min(2, "Please enter your company name"),
    type: z.enum(["full-time", "part-time", "contract", "internship", "temporary"]),
    experience_level: z.enum(["entry", "mid", "senior", "executive"]),
    number_of_openings: z.number().int().min(1, "You need at least 1 opening").max(999, "Maximum 999 openings allowed"),
    category_id: z.string().optional(),
    industry_id: z.string().optional(),
    tags: z.array(z.string()).max(10, "You can add up to 10 tags"),

    // Step 2: Location & Work Setup
    work_location_type: z.enum(["on-site", "remote", "hybrid"]),

    // Conditionally required based on work_location_type (handled in the form component or via refine)
    address: z.string().optional(),
    city: z.string().optional(),
    state: z.string().optional(),
    country: z.string().optional(),
    zip_code: z.string().optional(),

    remote_geo_restriction: z.string().optional(),
    time_zone_requirements: z.string().optional(),
    days_in_office: z.number().min(1).max(7).optional(),

    // Step 3: Compensation & Benefits
    hide_salary: z.boolean().default(false),
    is_fixed_salary: z.boolean().default(false),
    salary_type: z.enum(["hourly", "monthly", "annual", "contract", "commission"]).optional(),
    salary_min: z.number().min(0, "Minimum pay cannot be negative").optional(),
    salary_max: z.number().min(0, "Maximum pay cannot be negative").optional(),
    currency: z.string().default("NGN"),
    additional_compensation: z.array(z.string()).optional(),
    benefits: z.array(z.string()).optional(),

    // Step 4: Description & Requirements
    summary: z.string().max(200, "Summary is too long — keep it under 200 characters").optional(),
    description: z.string().min(1, "Please write a job description"),
    responsibilities: z.array(z.string()).min(1, "Please add at least one responsibility"),
    required_qualifications: z.array(z.string()).min(1, "Please add at least one required qualification"),
    preferred_qualifications: z.array(z.string()).optional(),

    // Skills are linked in a join table, but we capture the names here
    skills: z.array(
        z.object({
            name: z.string(),
            is_required: z.boolean(),
        })
    ).max(15, "Maximum 15 skills allowed"),

    // Step 5: Application Settings
    application_method: z.enum(["internal", "external", "email", "phone"]),
    external_url: z.string().url("Must be a valid URL").optional(),
    contact_email: z.string().email("Must be a valid email").optional(),
    deadline: z.date().optional(),

    // Screening Questions
    screening_questions: z.array(ScreeningQuestionSchema).optional(),

    // Internal Tracking
    internal_notes: z.string().optional(),
}).superRefine((data, ctx) => {
    // Validate Salary Range
    if (!data.is_fixed_salary && data.salary_min && data.salary_max && data.salary_min > data.salary_max) {
        ctx.addIssue({
            code: z.ZodIssueCode.custom,
            message: "The maximum salary should be higher than the minimum salary",
            path: ["salary_max"],
        });
    }

    // Validate Application Methods
    if (data.application_method === "external" && !data.external_url) {
        ctx.addIssue({
            code: z.ZodIssueCode.custom,
            message: "Please enter the external application URL",
            path: ["external_url"],
        });
    }

    if (data.application_method === "email" && !data.contact_email) {
        ctx.addIssue({
            code: z.ZodIssueCode.custom,
            message: "Please enter the contact email address for applications",
            path: ["contact_email"],
        });
    }

    // Validate Location requirements based on type
    if ((data.work_location_type === "on-site" || data.work_location_type === "hybrid") && !data.city) {
        ctx.addIssue({
            code: z.ZodIssueCode.custom,
            message: "Please enter the city where this role is based",
            path: ["city"],
        });
    }
});

export type JobPostingFormValues = z.infer<typeof JobPostingSchema>;
export type ScreeningQuestionValues = z.infer<typeof ScreeningQuestionSchema>;
