-- CreateEnum
CREATE TYPE "CrewStatus" AS ENUM ('PROSPECT', 'CANDIDATE', 'EMPLOYEE', 'EX_HAND', 'BLACKLISTED');

-- CreateEnum
CREATE TYPE "CandidateType" AS ENUM ('NEW', 'EX_HAND');

-- CreateEnum
CREATE TYPE "CandidateSource" AS ENUM ('CAREERS', 'EX_HAND', 'WALK_IN', 'REFERRAL', 'OTHER');

-- AlterEnum
-- This migration adds more than one value to an enum.
-- With PostgreSQL versions 11 and earlier, this is not possible
-- in a single migration. This can be worked around by creating
-- multiple migrations, each migration adding only one value to
-- the enum.


ALTER TYPE "CrewActionType" ADD VALUE 'CANDIDATE_ADDED';
ALTER TYPE "CrewActionType" ADD VALUE 'CANDIDATE_UPDATED';

-- AlterTable
ALTER TABLE "CrewAction" ADD COLUMN     "crewMemberId" TEXT;

-- CreateTable
CREATE TABLE "CrewMember" (
    "id" TEXT NOT NULL,
    "employeeId" TEXT,
    "name" TEXT NOT NULL,
    "status" "CrewStatus" NOT NULL DEFAULT 'CANDIDATE',
    "type" "CandidateType" NOT NULL DEFAULT 'NEW',
    "source" "CandidateSource" NOT NULL DEFAULT 'CAREERS',
    "email" TEXT,
    "phone" TEXT,
    "dob" TIMESTAMP(3),
    "experienceMonths" INTEGER NOT NULL DEFAULT 0,
    "vesselTypeExperience" TEXT,
    "cvKey" TEXT,
    "notes" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "currentRankId" TEXT,
    "appliedRankId" TEXT,

    CONSTRAINT "CrewMember_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "CrewMember_employeeId_key" ON "CrewMember"("employeeId");

-- AddForeignKey
ALTER TABLE "CrewAction" ADD CONSTRAINT "CrewAction_crewMemberId_fkey" FOREIGN KEY ("crewMemberId") REFERENCES "CrewMember"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CrewMember" ADD CONSTRAINT "CrewMember_currentRankId_fkey" FOREIGN KEY ("currentRankId") REFERENCES "Rank"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CrewMember" ADD CONSTRAINT "CrewMember_appliedRankId_fkey" FOREIGN KEY ("appliedRankId") REFERENCES "Rank"("id") ON DELETE SET NULL ON UPDATE CASCADE;
