DROP TABLE IF EXISTS company_reports; -- it's empty

DO $$
BEGIN
    DO $a$
    BEGIN
        CREATE TYPE department_breakdown_item AS (
            "department" VARCHAR,
            "count" int
        );
        CREATE TYPE company_report_overview AS (
            "total_employees" int,
            "department_breakdown" department_breakdown_item[],
            "submissionRate" double precision,
            "last_updated" TIMESTAMP,
            "average_performance_score" double precision,
            "risk_level" VARCHAR
        );
        CREATE TYPE weaknesses AS (
            "title" VARCHAR,
            "description" TEXT
        );
        CREATE TYPE new_hire AS (
            "name" VARCHAR,
            "department" VARCHAR,
            "role" VARCHAR,
            "impact" TEXT
        );
        CREATE TYPE promotion AS (
            "name" VARCHAR,
            "from_role" VARCHAR,
            "to_role" VARCHAR,
            "impact" TEXT
        );
        CREATE TYPE departure AS (
            "name" VARCHAR,
            "department" VARCHAR,
            "reason" TEXT,
            "impact" TEXT

        );
        CREATE TYPE personnel_changes AS (
            "new_hires" new_hire[],
            "promotions" promotion[],
            "departures" departure[]
        );
        CREATE TYPE immediate_hiring_needs AS (
            "department" VARCHAR,
            "role" VARCHAR,
            "priority" VARCHAR,
            "reasoning" TEXT
        );
        CREATE TYPE forward_operating_plan AS (
            "title" VARCHAR,
            "details" TEXT
        );
        CREATE TYPE organizational_impact_summary_employee AS (
            "employee_name" VARCHAR,
            "impact" TEXT,
            "description" TEXT,
            "suggested_pay" double precision
        );
        CREATE TYPE organizational_impact_summary AS (
            "category" VARCHAR,
            "employees" organizational_impact_summary_employee[]
        );
        CREATE TYPE team_score AS (
            "employee_name" VARCHAR,
            "grade" VARCHAR,
            "reliability" double precision,
            "role_fit" double precision,
            "scalability" double precision,
            "output" double precision,
            "initiative" double precision
        );
        CREATE TYPE grading_breakdown AS (
            "department_name_short" VARCHAR,
            "department_name" VARCHAR,
            "lead" VARCHAR,
            "support" VARCHAR,
            "department_grade" VARCHAR,
            "executive_summary" TEXT,
            "team_scores" team_score[]
        );
    EXCEPTION
        WHEN duplicate_object THEN RAISE NOTICE 'Type department_breakdown_item or company_report_overview already exists, skipping creation.';
    END
    $a$ LANGUAGE plpgsql;

END
$$ LANGUAGE plpgsql;

CREATE TABLE IF NOT EXISTS company_reports (
    "id" SERIAL PRIMARY KEY,
    "organization_id" VARCHAR NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
    "prompt_used" TEXT,
    "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "overview" company_report_overview,
    "strengths" TEXT[],
    "weaknesses" weaknesses[],
    "personnel_changes" personnel_changes,
    "immediate_hiring_needs" immediate_hiring_needs[],
    "forward_operating_plan" forward_operating_plan[],
    "organizational_impact_summary" organizational_impact_summary[],
    "grading_breakdown" grading_breakdown[]
);
