import { r as __toESM } from "../_runtime.mjs";
import { a as require_react, i as require_jsx_runtime, r as useQueryClient, t as useQuery } from "../_libs/react+tanstack__react-query.mjs";
import { _ as useNavigate, h as Link } from "../_libs/@tanstack/react-router+[...].mjs";
import { i as SiteLayout, t as PageBody } from "./Layout-mJm59hV1.mjs";
import { t as supabase } from "./client-TD7udZC7.mjs";
import { t as Route } from "./admin-DyIgmoMd.mjs";
import { a as isAdmin, n as fetchApplications, o as slugify, t as fetchAllJobs } from "./jobs-DK-Pa4Ss.mjs";
import { n as toast } from "../_libs/sonner.mjs";
//#region node_modules/.nitro/vite/services/ssr/assets/admin-Bd8VIH2f.js
var import_react = /* @__PURE__ */ __toESM(require_react());
var import_jsx_runtime = require_jsx_runtime();
var empty = {
	title: "",
	department: "Gallery",
	location: "Boulder, Colorado",
	employment_type: "Full-time",
	salary_range: "",
	summary: "",
	description: "",
	requirements: "",
	benefits: "",
	how_to_apply: "Apply using the form on this listing. We respond to every application.",
	is_published: true
};
function Page() {
	const { user } = Route.useRouteContext();
	const navigate = useNavigate();
	const qc = useQueryClient();
	const [tab, setTab] = (0, import_react.useState)("jobs");
	const [editing, setEditing] = (0, import_react.useState)(null);
	const [form, setForm] = (0, import_react.useState)({ ...empty });
	const [busy, setBusy] = (0, import_react.useState)(false);
	const admin = useQuery({
		queryKey: ["is-admin", user.id],
		queryFn: () => isAdmin(user.id)
	});
	const jobs = useQuery({
		queryKey: ["jobs", "all"],
		queryFn: fetchAllJobs,
		enabled: admin.data === true
	});
	const apps = useQuery({
		queryKey: ["applications"],
		queryFn: fetchApplications,
		enabled: admin.data === true && tab === "applications"
	});
	async function signOut() {
		await qc.cancelQueries();
		qc.clear();
		await supabase.auth.signOut();
		navigate({
			to: "/auth",
			replace: true
		});
	}
	function startEdit(job) {
		setEditing(job);
		setForm({
			title: job.title,
			department: job.department,
			location: job.location,
			employment_type: job.employment_type,
			salary_range: job.salary_range ?? "",
			summary: job.summary,
			description: job.description,
			requirements: job.requirements,
			benefits: job.benefits,
			how_to_apply: job.how_to_apply,
			is_published: job.is_published
		});
		window.scrollTo({
			top: 0,
			behavior: "smooth"
		});
	}
	async function save(e) {
		e.preventDefault();
		if (!form.title.trim() || !form.summary.trim()) {
			toast.error("Title and summary are required.");
			return;
		}
		setBusy(true);
		const payload = {
			...form,
			salary_range: form.salary_range.trim() || null,
			slug: editing ? editing.slug : slugify(form.title)
		};
		const { error } = editing ? await supabase.from("jobs").update(payload).eq("id", editing.id) : await supabase.from("jobs").insert(payload);
		setBusy(false);
		if (error) {
			toast.error(error.message);
			return;
		}
		toast.success(editing ? "Job updated" : "Job posted");
		setEditing(null);
		setForm({ ...empty });
		qc.invalidateQueries({ queryKey: ["jobs"] });
	}
	async function togglePublish(job) {
		const { error } = await supabase.from("jobs").update({ is_published: !job.is_published }).eq("id", job.id);
		if (error) {
			toast.error(error.message);
			return;
		}
		qc.invalidateQueries({ queryKey: ["jobs"] });
	}
	async function remove(job) {
		if (!window.confirm(`Delete "${job.title}"? This cannot be undone.`)) return;
		const { error } = await supabase.from("jobs").delete().eq("id", job.id);
		if (error) {
			toast.error(error.message);
			return;
		}
		toast.success("Job deleted");
		qc.invalidateQueries({ queryKey: ["jobs"] });
	}
	if (admin.isPending) return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SiteLayout, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PageBody, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", {
		className: "text-muted-foreground",
		children: "Checking access…"
	}) }) });
	if (!admin.data) return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SiteLayout, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(PageBody, { children: [
		/* @__PURE__ */ (0, import_jsx_runtime.jsx)("h1", {
			className: "font-display text-3xl text-maroon",
			children: "Admin access required"
		}),
		/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", {
			className: "mt-3 max-w-xl text-[17px] leading-relaxed",
			children: [
				"You are signed in as ",
				user.email,
				", but this account does not have the admin role. Ask an existing administrator to grant access."
			]
		}),
		/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
			className: "mt-6 flex gap-3",
			children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
				onClick: signOut,
				className: "border border-border px-5 py-2.5 text-sm uppercase hover:bg-accent",
				children: "Sign out"
			}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Link, {
				to: "/",
				className: "bg-maroon px-5 py-2.5 text-sm uppercase text-primary-foreground",
				children: "Back to site"
			})]
		})
	] }) });
	return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SiteLayout, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(PageBody, { children: [
		/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
			className: "flex flex-wrap items-center justify-between gap-4",
			children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("h1", {
				className: "font-display text-4xl text-maroon",
				children: "Careers admin"
			}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
				className: "flex items-center gap-3 text-sm",
				children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
					className: "text-muted-foreground",
					children: user.email
				}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
					onClick: signOut,
					className: "border border-border px-4 py-2 uppercase hover:bg-accent",
					children: "Sign out"
				})]
			})]
		}),
		/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
			className: "mt-6 flex gap-2 border-b border-border",
			children: ["jobs", "applications"].map((t) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
				onClick: () => setTab(t),
				className: `px-5 py-2.5 text-sm uppercase tracking-wide ${tab === t ? "border-b-2 border-maroon text-maroon" : "text-muted-foreground hover:text-maroon"}`,
				children: t
			}, t))
		}),
		tab === "jobs" ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
			className: "mt-8 grid gap-10 lg:grid-cols-[420px_1fr]",
			children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("form", {
				onSubmit: save,
				className: "h-fit border border-border bg-card p-6 shadow-sm",
				children: [
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)("h2", {
						className: "font-display text-2xl text-maroon",
						children: editing ? "Edit job" : "Post a new job"
					}),
					/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
						className: "mt-4 space-y-3",
						children: [
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Text, {
								label: "Title",
								value: form.title,
								onChange: (v) => setForm({
									...form,
									title: v
								})
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Text, {
								label: "Department",
								value: form.department,
								onChange: (v) => setForm({
									...form,
									department: v
								})
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Text, {
								label: "Location",
								value: form.location,
								onChange: (v) => setForm({
									...form,
									location: v
								})
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Text, {
								label: "Employment type",
								value: form.employment_type,
								onChange: (v) => setForm({
									...form,
									employment_type: v
								})
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Text, {
								label: "Salary range",
								value: form.salary_range,
								onChange: (v) => setForm({
									...form,
									salary_range: v
								})
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Area, {
								label: "Summary",
								value: form.summary,
								rows: 3,
								onChange: (v) => setForm({
									...form,
									summary: v
								})
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Area, {
								label: "Description",
								value: form.description,
								rows: 6,
								onChange: (v) => setForm({
									...form,
									description: v
								})
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Area, {
								label: "Requirements",
								value: form.requirements,
								rows: 5,
								onChange: (v) => setForm({
									...form,
									requirements: v
								})
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Area, {
								label: "Benefits",
								value: form.benefits,
								rows: 4,
								onChange: (v) => setForm({
									...form,
									benefits: v
								})
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Area, {
								label: "How to apply",
								value: form.how_to_apply,
								rows: 3,
								onChange: (v) => setForm({
									...form,
									how_to_apply: v
								})
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("label", {
								className: "flex items-center gap-2 text-sm",
								children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", {
									type: "checkbox",
									checked: form.is_published,
									onChange: (e) => setForm({
										...form,
										is_published: e.target.checked
									})
								}), "Published on the careers page"]
							})
						]
					}),
					/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
						className: "mt-5 flex gap-2",
						children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
							type: "submit",
							disabled: busy,
							className: "bg-maroon px-5 py-2.5 text-sm uppercase text-primary-foreground hover:bg-maroon-dark disabled:opacity-60",
							children: busy ? "Saving…" : editing ? "Save changes" : "Post job"
						}), editing && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
							type: "button",
							onClick: () => {
								setEditing(null);
								setForm({ ...empty });
							},
							className: "border border-border px-5 py-2.5 text-sm uppercase hover:bg-accent",
							children: "Cancel"
						})]
					})
				]
			}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
				className: "space-y-4",
				children: [jobs.data?.map((job) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
					className: "border border-border bg-card p-5 shadow-sm",
					children: [
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
							className: "flex flex-wrap items-baseline justify-between gap-2",
							children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("h3", {
								className: "font-display text-xl text-maroon",
								children: job.title
							}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
								className: `text-xs uppercase tracking-wide ${job.is_published ? "text-gold-dark" : "text-muted-foreground"}`,
								children: job.is_published ? "Published" : "Draft"
							})]
						}),
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", {
							className: "mt-1 text-sm text-muted-foreground",
							children: [
								job.department,
								" · ",
								job.location,
								" · ",
								job.employment_type
							]
						}),
						/* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", {
							className: "mt-2 text-[15px] text-foreground/85",
							children: job.summary
						}),
						/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
							className: "mt-3 flex flex-wrap gap-2 text-xs uppercase",
							children: [
								/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
									onClick: () => startEdit(job),
									className: "border border-border px-3 py-1.5 hover:bg-accent",
									children: "Edit"
								}),
								/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
									onClick: () => togglePublish(job),
									className: "border border-border px-3 py-1.5 hover:bg-accent",
									children: job.is_published ? "Unpublish" : "Publish"
								}),
								/* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
									onClick: () => remove(job),
									className: "border border-destructive px-3 py-1.5 text-destructive hover:bg-destructive/10",
									children: "Delete"
								}),
								/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Link, {
									to: "/careers/$slug",
									params: { slug: job.slug },
									className: "border border-border px-3 py-1.5 hover:bg-accent",
									children: "View"
								})
							]
						})
					]
				}, job.id)), jobs.data?.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", {
					className: "text-muted-foreground",
					children: "No jobs yet — post the first one."
				})]
			})]
		}) : /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
			className: "mt-8 space-y-4",
			children: [
				apps.isPending && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", {
					className: "text-muted-foreground",
					children: "Loading applications…"
				}),
				apps.data?.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", {
					className: "text-muted-foreground",
					children: "No applications yet."
				}),
				apps.data?.map((a) => {
					const row = a;
					return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("article", {
						className: "border border-border bg-card p-5 shadow-sm",
						children: [
							/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
								className: "flex flex-wrap items-baseline justify-between gap-2",
								children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("h3", {
									className: "font-display text-xl text-maroon",
									children: row.full_name
								}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
									className: "text-sm text-muted-foreground",
									children: new Date(row.created_at).toLocaleDateString()
								})]
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", {
								className: "mt-1 text-sm text-muted-foreground",
								children: [
									row.jobs?.title ?? "Unknown role",
									" · ",
									row.email,
									row.phone ? ` · ${row.phone}` : ""
								]
							}),
							row.resume_path ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ResumeButton, { path: row.resume_path }) : row.resume_url ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("a", {
								href: row.resume_url,
								className: "mt-1 inline-block text-sm text-maroon underline",
								children: "Résumé link"
							}) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", {
								className: "mt-1 inline-block text-sm text-muted-foreground",
								children: "No résumé attached"
							}),
							/* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", {
								className: "mt-3 whitespace-pre-wrap text-[15px] leading-relaxed text-foreground/85",
								children: row.cover_letter
							})
						]
					}, row.id);
				})
			]
		})
	] }) });
}
function ResumeButton({ path }) {
	const [loading, setLoading] = (0, import_react.useState)(false);
	async function open() {
		setLoading(true);
		const { data, error } = await supabase.storage.from("resumes").createSignedUrl(path, 300);
		setLoading(false);
		if (error || !data?.signedUrl) {
			toast.error("Could not open that résumé.");
			return;
		}
		window.open(data.signedUrl, "_blank", "noopener");
	}
	return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", {
		onClick: open,
		disabled: loading,
		className: "mt-2 inline-block border border-border px-3 py-1.5 text-xs uppercase hover:bg-accent disabled:opacity-60",
		children: loading ? "Opening…" : "Download résumé"
	});
}
function Text({ label, value, onChange }) {
	return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("label", {
		className: "block text-sm font-semibold text-maroon",
		children: label
	}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", {
		value,
		onChange: (e) => onChange(e.target.value),
		maxLength: 200,
		className: "mt-1 w-full border border-input bg-background px-3 py-2 text-[15px] outline-none focus:border-gold"
	})] });
}
function Area({ label, value, rows, onChange }) {
	return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("label", {
		className: "block text-sm font-semibold text-maroon",
		children: label
	}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("textarea", {
		value,
		rows,
		onChange: (e) => onChange(e.target.value),
		maxLength: 5e3,
		className: "mt-1 w-full border border-input bg-background px-3 py-2 text-[15px] outline-none focus:border-gold"
	})] });
}
//#endregion
export { Page as component };
