import{s as r}from"./index-CzsKMdZJ.js";const j={async fetchAdminJobsLite(e={},o=1,i=25){let a=r.from("jobs").select(`
id, title, location, is_remote, salary_min, salary_max, salary_currency,
job_type, is_active, approval_status, total_applications, created_at, is_featured,
require_interviews, application_deadline,
companies!inner(name),
job_categories(name),
profiles!jobs_posted_by_fkey(first_name, last_name)
`),t=r.from("jobs").select("*",{count:"exact",head:!0});if(e.status&&e.status!=="all"&&(["pending","approved","rejected"].includes(e.status)?(a=a.eq("approval_status",e.status),t=t.eq("approval_status",e.status)):e.status==="active"?(a=a.eq("is_active",!0),t=t.eq("is_active",!0)):e.status==="draft"&&(a=a.eq("is_active",!1),t=t.eq("is_active",!1))),e.featured&&e.featured!=="all"){const s=e.featured==="featured";a=a.eq("is_featured",s),t=t.eq("is_featured",s)}if(e.search){const s=`%${e.search.toLowerCase()}%`;a=a.or(`title.ilike.${s},location.ilike.${s}`),t=t.or(`title.ilike.${s},location.ilike.${s}`)}if(e.category&&(a=a.eq("category_id",e.category),t=t.eq("category_id",e.category)),e.deadlineStatus&&e.deadlineStatus!=="all"){const s=new Date().toISOString();e.deadlineStatus==="expired"?(a=a.lt("application_deadline",s),t=t.lt("application_deadline",s)):e.deadlineStatus==="live"&&(a=a.or(`application_deadline.gte.${s},application_deadline.is.null`),t=t.or(`application_deadline.gte.${s},application_deadline.is.null`))}const{count:n}=await t,c=(o-1)*i,{data:l,error:d}=await a.order("created_at",{ascending:!1}).range(c,c+i-1);if(d)throw d;return{data:(l==null?void 0:l.map(s=>{var p,u;return{id:s.id,title:s.title,location:s.location,is_remote:s.is_remote,salary_min:s.salary_min,salary_max:s.salary_max,salary_currency:s.salary_currency,job_type:s.job_type,is_active:s.is_active,approval_status:s.approval_status,total_applications:s.total_applications,created_at:s.created_at,is_featured:s.is_featured,application_deadline:s.application_deadline,require_interviews:s.require_interviews??!0,company_name:((p=s.companies)==null?void 0:p.name)||"Unknown Company",category_name:((u=s.job_categories)==null?void 0:u.name)||null,poster_name:s.profiles?`${s.profiles.first_name} ${s.profiles.last_name}`.trim():"Unknown"}}))||[],count:n||0}},async fetchJobsForAdmin(e={},o=1,i=10){let a=r.from("jobs").select(`
*,
company:companies(*),
category:job_categories(*),
skills:job_skills(*),
poster:profiles!jobs_posted_by_fkey(first_name, last_name, email)
`),t=r.from("jobs").select("*",{count:"exact",head:!0});if(e.status&&e.status!=="all"&&(["pending","approved","rejected"].includes(e.status)?(a=a.eq("approval_status",e.status),t=t.eq("approval_status",e.status)):e.status==="active"?(a=a.eq("is_active",!0),t=t.eq("is_active",!0)):e.status==="draft"&&(a=a.eq("is_active",!1),t=t.eq("is_active",!1))),e.featured&&e.featured!=="all"&&(e.featured==="featured"?(a=a.eq("is_featured",!0),t=t.eq("is_featured",!0)):e.featured==="not-featured"&&(a=a.eq("is_featured",!1),t=t.eq("is_featured",!1))),e.search){const _=e.search.toLowerCase(),{data:s}=await r.from("jobs").select(`
id,
companies!inner(name)
`).eq("is_active",!0).or(`title.ilike.%${_}%,description.ilike.%${_}%,location.ilike.%${_}%,companies.name.ilike.%${_}%`);if(s&&s.length>0){const p=s.map(u=>u.id);a=a.in("id",p),t=t.in("id",p)}else return{data:[],count:0}}e.location&&(a=a.ilike("location",`%${e.location}%`),t=t.ilike("location",`%${e.location}%`)),e.category&&(a=a.eq("category_id",e.category),t=t.eq("category_id",e.category)),e.jobType&&(a=a.eq("job_type",e.jobType),t=t.eq("job_type",e.jobType)),e.isRemote!==void 0&&(a=a.eq("is_remote",e.isRemote),t=t.eq("is_remote",e.isRemote)),e.minSalary&&(a=a.gte("salary_min",e.minSalary),t=t.gte("salary_min",e.minSalary)),e.maxSalary&&(a=a.lte("salary_max",e.maxSalary),t=t.lte("salary_max",e.maxSalary));const{count:n}=await t,c=(o-1)*i,{data:l,error:d}=await a.order("created_at",{ascending:!1}).range(c,c+i-1);if(d)throw d;return{data:l,count:n||0}},async checkJobStatus(e){const{data:o,error:i}=await r.from("jobs").select("id, title, is_active, approval_status").eq("id",e).eq("approval_status","approved").not("posted_by","is",null).single();if(i){if(i.code==="PGRST116")return{exists:!1,isActive:!1};throw i}return{exists:!0,isActive:o.is_active,title:o.title}},async fetchJobById(e){const{data:o,error:i}=await r.from("jobs").select(`
id, title, description, requirements, benefits,
salary_min, salary_max, salary_currency, job_type,
experience_level, location, is_remote, application_deadline,
max_applications, total_applications, is_featured, is_active,
approval_status, category_id, company_id, interview_questions,
minimum_hire_score, share_url, share_slug,
created_at, updated_at,
company:companies(
id, name, description, logo_url, banner_url, website,
industry, headquarters, founded_year, company_size,
total_jobs, is_verified, created_at, updated_at
),
category:job_categories(*),
skills:job_skills(*)
`).eq("id",e).eq("is_active",!0).eq("approval_status","approved").not("posted_by","is",null).single();if(i){if(i.code==="PGRST116")return null;throw i}return o},async fetchAdminJobById(e){const{data:o,error:i}=await r.from("jobs").select(`
*,
company:companies(*),
category:job_categories(*),
skills:job_skills(*),
poster:profiles!jobs_posted_by_fkey(first_name, last_name, email),
reviewer:profiles!jobs_reviewed_by_fkey(first_name, last_name, email)
`).eq("id",e).single();if(i){if(i.code==="PGRST116")return null;throw i}return o},async fetchFeaturedJobs(e=6){const{data:o,error:i}=await r.from("jobs").select(`
id, title, description, requirements, benefits,
salary_min, salary_max, salary_currency, job_type,
experience_level, location, is_remote, application_deadline,
max_applications, total_applications, is_featured, is_active,
approval_status, category_id, company_id, interview_questions,
minimum_hire_score, share_url, share_slug,
created_at, updated_at,
company:companies(
id, name, description, logo_url, banner_url, website,
industry, headquarters, founded_year, company_size,
total_jobs, is_verified, created_at, updated_at
),
category:job_categories(*),
skills:job_skills(*)
`).eq("is_active",!0).eq("is_featured",!0).eq("approval_status","approved").not("posted_by","is",null).or("application_deadline.is.null,application_deadline.gte."+new Date().toISOString().split("T")[0]).order("created_at",{ascending:!1}).limit(e);if(i)throw i;return o},async fetchJobCategories(){const{data:e,error:o}=await r.from("job_categories").select("*").eq("is_active",!0).order("name");if(o)throw o;return e||[]},async fetchJobCategoriesAdmin(e={}){let o=r.from("job_categories").select("*").order("name");e.search&&(o=o.or(`name.ilike.%${e.search}%,description.ilike.%${e.search}%`));const{data:i,error:a}=await o;if(a)throw a;return await Promise.all((i||[]).map(async n=>{const{count:c}=await r.from("jobs").select("*",{count:"exact",head:!0}).eq("category_id",n.id).eq("is_active",!0);return{...n,job_count:c||0}}))},async getJobCategoryStats(){const{data:e,error:o}=await r.from("job_categories").select("is_active");if(o)throw o;const{count:i,error:a}=await r.from("jobs").select("*",{count:"exact",head:!0}).eq("is_active",!0);if(a)throw a;const t=(e==null?void 0:e.length)||0,n=(e==null?void 0:e.filter(l=>l.is_active).length)||0,c=t-n;return{total:t,active:n,inactive:c,totalJobs:i||0}},async createJobCategory(e){const o=e.name.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/(^-|-$)/g,""),{data:i,error:a}=await r.from("job_categories").insert({...e,slug:o,job_count:0}).select().single();if(a)throw a;return i},async updateJobCategory(e,o){const i={...o};o.name&&(i.slug=o.name.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/(^-|-$)/g,""));const{data:a,error:t}=await r.from("job_categories").update(i).eq("id",e).select().single();if(t)throw t;return a},async deleteJobCategory(e){const{error:o}=await r.from("job_categories").delete().eq("id",e);if(o)throw o},async searchJobs(e,o=20){const i=e.toLowerCase(),{data:a,error:t}=await r.from("jobs").select(`
id,
companies!inner(name)
`).eq("is_active",!0).not("posted_by","is",null).or("application_deadline.is.null,application_deadline.gte."+new Date().toISOString().split("T")[0]).or(`title.ilike.%${i}%,description.ilike.%${i}%,location.ilike.%${i}%,companies.name.ilike.%${i}%`).limit(o);if(t)throw t;if(!a||a.length===0)return[];const n=a.map(d=>d.id),{data:c,error:l}=await r.from("jobs").select(`
id, title, description, requirements, benefits,
salary_min, salary_max, salary_currency, job_type,
experience_level, location, is_remote, application_deadline,
max_applications, total_applications, is_featured, is_active,
approval_status, category_id, company_id, interview_questions,
minimum_hire_score, share_url, share_slug,
created_at, updated_at,
company:companies(
id, name, description, logo_url, banner_url, website,
industry, headquarters, founded_year, company_size,
total_jobs, is_verified, created_at, updated_at
),
category:job_categories(*),
skills:job_skills(*)
`).eq("approval_status","approved").or("application_deadline.is.null,application_deadline.gte."+new Date().toISOString().split("T")[0]).in("id",n).order("created_at",{ascending:!1});if(l)throw l;return c},async fetchRelatedJobs(e,o,i,a=4){let t=r.from("jobs").select(`
id, title, description, requirements, benefits,
salary_min, salary_max, salary_currency, job_type,
experience_level, location, is_remote, application_deadline,
max_applications, total_applications, is_featured, is_active,
approval_status, category_id, company_id, interview_questions,
minimum_hire_score, share_url, share_slug,
created_at, updated_at,
company:companies(
id, name, description, logo_url, banner_url, website,
industry, headquarters, founded_year, company_size,
total_jobs, is_verified, created_at, updated_at
),
category:job_categories(*),
skills:job_skills(*)
`).eq("is_active",!0).eq("approval_status","approved").not("posted_by","is",null).or("application_deadline.is.null,application_deadline.gte."+new Date().toISOString().split("T")[0]).neq("id",e);o?t=t.eq("company_id",o):i&&(t=t.eq("category_id",i));const{data:n,error:c}=await t.order("created_at",{ascending:!1}).limit(a);if(c)throw c;return n},async getJobApplications(e){const{data:o,error:i}=await r.from("job_applications").select(`
*,
applicant:profiles!applicant_id (
id,
first_name,
last_name,
email,
avatar_url
),
job:jobs!job_id (
id,
title,
posted_by
)
`).eq("job_id",e).order("applied_at",{ascending:!1});if(i)throw i;return o||[]},async fetchJobs(e={},o=1,i=12){let a=r.from("jobs").select(`
id, title, description, requirements, benefits,
salary_min, salary_max, salary_currency, job_type,
experience_level, location, is_remote, application_deadline,
max_applications, total_applications, is_featured, is_active,
approval_status, category_id, company_id, interview_questions,
minimum_hire_score, share_url, share_slug,
created_at, updated_at,
company:companies(
id, name, description, logo_url, banner_url, website,
industry, headquarters, founded_year, company_size,
total_jobs, is_verified, created_at, updated_at
),
category:job_categories(*),
skills:job_skills(*)
`).eq("is_active",!0).eq("approval_status","approved").not("posted_by","is",null).or("application_deadline.is.null,application_deadline.gte."+new Date().toISOString().split("T")[0]),t=null;if(e.search){const s=e.search.toLowerCase(),{data:p}=await r.from("jobs").select("id").eq("is_active",!0).eq("approval_status","approved").not("posted_by","is",null).or("application_deadline.is.null,application_deadline.gte."+new Date().toISOString().split("T")[0]).or(`title.ilike.%${s}%,description.ilike.%${s}%,location.ilike.%${s}%`),{data:u}=await r.from("jobs").select("id, companies!inner(name)").eq("is_active",!0).eq("approval_status","approved").not("posted_by","is",null).or("application_deadline.is.null,application_deadline.gte."+new Date().toISOString().split("T")[0]).ilike("companies.name",`%${s}%`),y=new Set([...(p||[]).map(m=>m.id),...(u||[]).map(m=>m.id)]);if(y.size>0)t=Array.from(y),a=a.in("id",t);else return{data:[],count:0}}e.location&&(a=a.ilike("location",`%${e.location}%`)),e.category&&(a=a.eq("category_id",e.category)),e.jobType&&(a=a.eq("job_type",e.jobType)),e.isRemote!==void 0&&(a=a.eq("is_remote",e.isRemote)),e.minSalary&&(a=a.gte("salary_min",e.minSalary)),e.maxSalary&&(a=a.lte("salary_max",e.maxSalary)),e.experienceLevel&&(a=a.eq("experience_level",e.experienceLevel));let n=r.from("jobs").select("*",{count:"exact",head:!0}).eq("is_active",!0).eq("approval_status","approved").not("posted_by","is",null).or("application_deadline.is.null,application_deadline.gte."+new Date().toISOString().split("T")[0]);if(e.search)if(t&&t.length>0)n=n.in("id",t);else return{data:[],count:0};e.location&&(n=n.ilike("location",`%${e.location}%`)),e.category&&(n=n.eq("category_id",e.category)),e.jobType&&(n=n.eq("job_type",e.jobType)),e.isRemote!==void 0&&(n=n.eq("is_remote",e.isRemote)),e.minSalary&&(n=n.gte("salary_min",e.minSalary)),e.maxSalary&&(n=n.lte("salary_max",e.maxSalary)),e.experienceLevel&&(n=n.eq("experience_level",e.experienceLevel));const{count:c}=await n,l=(o-1)*i,{data:d,error:_}=await a.order("created_at",{ascending:!1}).range(l,l+i-1);if(_)throw _;return{data:d,count:c||0}},async getJobStats(){const[e,o,i,a,t,n,c,l]=await Promise.all([r.from("jobs").select("*",{count:"exact",head:!0}).not("company_id","is",null),r.from("jobs").select("*",{count:"exact",head:!0}).eq("is_active",!0).not("company_id","is",null),r.from("job_applications").select("*",{count:"exact",head:!0}),r.from("interview_sessions").select("*",{count:"exact",head:!0}),r.from("companies").select("*",{count:"exact",head:!0}),r.from("companies").select("*",{count:"exact",head:!0}).eq("is_verified",!0),r.from("company_locations").select("*",{count:"exact",head:!0}),r.from("profiles").select("*",{count:"exact",head:!0})]),d=await r.from("jobs").select("*",{count:"exact",head:!0}).eq("is_active",!1).not("company_id","is",null),_=new Date().getFullYear(),p=Math.max(1,_-2023+1);return{total:e.count||0,active:o.count||0,draft:d.count||0,applications:i.count||0,interviews:a.count||0,companies:t.count||0,verifiedCompanies:n.count||0,companyLocations:c.count||0,totalUsers:l.count||0,platformYears:p}},async getJobApprovalStats(){const[e,o,i,a]=await Promise.all([r.from("jobs").select("*",{count:"exact",head:!0}).eq("approval_status","pending").not("company_id","is",null),r.from("jobs").select("*",{count:"exact",head:!0}).eq("approval_status","approved").not("company_id","is",null),r.from("jobs").select("*",{count:"exact",head:!0}).eq("approval_status","rejected").not("company_id","is",null),r.from("jobs").select("*",{count:"exact",head:!0}).not("company_id","is",null)]);return{pending:e.count||0,approved:o.count||0,rejected:i.count||0,total:a.count||0}},async fetchJobsForApproval(e={},o=1,i=12){let a=r.from("jobs").select(`
*,
company:companies(*),
category:job_categories(*),
skills:job_skills(*),
reviewer:profiles!jobs_reviewed_by_fkey(first_name, last_name, email),
poster:profiles!jobs_posted_by_fkey(first_name, last_name, email)
`,{count:"exact"});e.status&&e.status!=="all"&&(a=a.eq("approval_status",e.status)),e.search&&(a=a.or(`title.ilike.%${e.search}%,description.ilike.%${e.search}%,location.ilike.%${e.search}%`));let t=r.from("jobs").select("*",{count:"exact",head:!0});e.status&&e.status!=="all"&&(t=t.eq("approval_status",e.status)),e.search&&(t=t.or(`title.ilike.%${e.search}%,description.ilike.%${e.search}%,location.ilike.%${e.search}%`));const{count:n}=await t,c=(o-1)*i,{data:l,error:d}=await a.order("created_at",{ascending:!1}).range(c,c+i-1);if(d)throw d;return{data:l,count:n||0}},async approveJob(e,o){var t;const{data:i,error:a}=await r.from("jobs").update({approval_status:"approved",review_notes:o||null,reviewed_by:(t=(await r.auth.getUser()).data.user)==null?void 0:t.id,reviewed_at:new Date().toISOString(),is_active:!0}).eq("id",e).select(`
*,
company:companies(*),
category:job_categories(*),
skills:job_skills(*)
`).single();if(a)throw a;return i},async rejectJob(e,o){var t;const{data:i,error:a}=await r.from("jobs").update({approval_status:"rejected",review_notes:o,reviewed_by:(t=(await r.auth.getUser()).data.user)==null?void 0:t.id,reviewed_at:new Date().toISOString(),is_active:!1}).eq("id",e).select(`
*,
company:companies(*),
category:job_categories(*),
skills:job_skills(*)
`).single();if(a)throw a;return i},async createJob(e){const o={...e,application_deadline:e.application_deadline||null,salary_min:e.salary_min||null,salary_max:e.salary_max||null,max_applications:e.max_applications||null,posted_by:e.posted_by,approval_status:"approved",is_active:!0},{data:i,error:a}=await r.from("jobs").insert(o).select(`
*,
company:companies(*),
category:job_categories(*),
skills:job_skills(*)
`).single();if(a)throw a;return i},async updateJob(e,o){const i={...o,application_deadline:o.application_deadline||null,salary_min:o.salary_min||null,salary_max:o.salary_max||null,max_applications:o.max_applications||null},{data:a,error:t}=await r.from("jobs").update(i).eq("id",e).select(`
*,
company:companies(*),
category:job_categories(*),
skills:job_skills(*)
`).single();if(t)throw t;return a},async toggleJobStatus(e){const{data:o,error:i}=await r.from("jobs").select("is_active").eq("id",e).single();if(i)throw i;const{data:a,error:t}=await r.from("jobs").update({is_active:!o.is_active}).eq("id",e).select(`
*,
company:companies(*),
category:job_categories(*),
skills:job_skills(*)
`).single();if(t)throw t;return a},async deleteJob(e){const{error:o}=await r.from("jobs").delete().eq("id",e);if(o)throw o},async fetchJobsForEmployer(e,o={},i=1,a=12){if(!e)return{data:[],count:0};let t=r.from("jobs").select(`
*,
company:companies(*),
category:job_categories(*),
skills:job_skills(*)
`).eq("posted_by",e),n=r.from("jobs").select("*",{count:"exact",head:!0}).eq("posted_by",e);if(o.search){const s=o.search.toLowerCase();t=t.or(`title.ilike.%${s}%,description.ilike.%${s}%,location.ilike.%${s}%`),n=n.or(`title.ilike.%${s}%,description.ilike.%${s}%,location.ilike.%${s}%`)}o.jobType&&(t=t.eq("job_type",o.jobType),n=n.eq("job_type",o.jobType)),o.isRemote!==void 0&&(t=t.eq("is_remote",o.isRemote),n=n.eq("is_remote",o.isRemote));const{count:c}=await n,l=(i-1)*a,{data:d,error:_}=await t.order("created_at",{ascending:!1}).range(l,l+a-1);if(_)throw _;return{data:d,count:c||0}},async getJobApplicationsEnhanced(e){const{data:o,error:i}=await r.from("job_applications").select(`
*,
profiles!applicant_id(
id,
first_name,
last_name,
email,
avatar_url,
bio,
linkedin_url,
github_url
),
cv_analysis(
id,
match_score,
alignment_summary,
keyword_match_rate,
years_relevant_experience,
education_match_level,
extracted_skills,
experience_summary,
education,
strengths,
gaps,
recommendations,
created_at
)
`).eq("job_id",e).order("applied_at",{ascending:!1});if(i)throw console.error("Error fetching enhanced job applications:",i),i;return!o||o.length===0?[]:o.map(t=>({...t,profiles:t.profiles?{...t.profiles,display_name:`${t.profiles.first_name||""} ${t.profiles.last_name||""}`.trim()||"Anonymous Candidate"}:null,cv_analysis:Array.isArray(t.cv_analysis)?t.cv_analysis[0]||null:t.cv_analysis||null}))},async fetchEmployerJobById(e,o){const{data:i,error:a}=await r.from("jobs").select(`
*,
company:companies(*),
category:job_categories(*),
skills:job_skills(*),
poster:profiles!jobs_posted_by_fkey(first_name, last_name, email)
`).eq("id",e).eq("posted_by",o).maybeSingle();if(a)throw a;return i}};async function b(e={}){let o=r.from("job_reports").select(`
*,
job:jobs(
id,
title,
company:companies(name)
),
reporter:profiles!job_reports_reported_by_fkey(
first_name,
last_name,
email
),
reviewer:profiles!job_reports_reviewed_by_fkey(
first_name,
last_name,
email
)
`).order("created_at",{ascending:!1});if(e.status&&e.status!=="all"&&(o=o.eq("status",e.status)),e.priority&&e.priority!=="all"&&(o=o.eq("priority",e.priority)),e.search&&e.search.trim()){const t=e.search.trim();o=o.or(`
report_reason.ilike.%${t}%,
report_details.ilike.%${t}%,
job.title.ilike.%${t}%,
job.company.name.ilike.%${t}%
`)}const{data:i,error:a}=await o;if(a)throw console.error("Error fetching reported jobs:",a),new Error(a.message);return i||[]}async function g(){const{data:e,error:o}=await r.from("job_reports").select("status").eq("status","pending");if(o)throw console.error("Error fetching report stats:",o),new Error(o.message);const{data:i,error:a}=await r.from("job_reports").select("status").eq("status","resolved");if(a)throw console.error("Error fetching resolved stats:",a),new Error(a.message);const{data:t,error:n}=await r.from("job_reports").select("status").eq("status","dismissed");if(n)throw console.error("Error fetching dismissed stats:",n),new Error(n.message);const{data:c,error:l}=await r.from("job_reports").select("id");if(l)throw console.error("Error fetching total stats:",l),new Error(l.message);return{pending:(e==null?void 0:e.length)||0,resolved:(i==null?void 0:i.length)||0,dismissed:(t==null?void 0:t.length)||0,total:(c==null?void 0:c.length)||0}}async function f(e,o){var t;const{data:i,error:a}=await r.from("job_reports").update({status:"resolved",reviewed_by:(t=(await r.auth.getUser()).data.user)==null?void 0:t.id,reviewed_at:new Date().toISOString(),review_notes:o}).eq("id",e).select().single();if(a)throw console.error("Error resolving report:",a),new Error(a.message);return i}async function v(e,o){var t;const{data:i,error:a}=await r.from("job_reports").update({status:"dismissed",reviewed_by:(t=(await r.auth.getUser()).data.user)==null?void 0:t.id,reviewed_at:new Date().toISOString(),review_notes:o}).eq("id",e).select().single();if(a)throw console.error("Error dismissing report:",a),new Error(a.message);return i}async function w(e,o,i){await f(e,i);const{data:a,error:t}=await r.from("jobs").update({is_active:!1}).eq("id",o).select().single();if(t)throw console.error("Error removing reported job:",t),new Error(t.message);return a}const q={fetchReportedJobs:b,getJobReportStats:g,resolveReport:f,dismissReport:v,removeReportedJob:w};export{q as a,j};