CREATE TABLE "legal_work_attachments" (
	"id" serial PRIMARY KEY NOT NULL,
	"legal_work_id" integer NOT NULL,
	"file_name" varchar(255) NOT NULL,
	"original_file_name" varchar(255) NOT NULL,
	"file_path" varchar(500) NOT NULL,
	"file_size" integer NOT NULL,
	"mime_type" varchar(100) NOT NULL,
	"is_auto_generated" boolean DEFAULT false NOT NULL,
	"attachment_type" varchar(50) DEFAULT 'request',
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "legal_work_requests" (
	"id" serial PRIMARY KEY NOT NULL,
	"legal_work_id" integer NOT NULL,
	"review_department" varchar(50) NOT NULL,
	"inquiry_type" varchar(50),
	"title" varchar(500) NOT NULL,
	"request_content" text NOT NULL,
	"is_public" boolean DEFAULT false,
	"contract_project_name" varchar(300),
	"contract_type" varchar(100),
	"contract_amount" numeric(15, 2),
	"contract_counterparty" varchar(200),
	"counterparty_type" varchar(20),
	"contract_period" varchar(200),
	"factual_relation" text,
	"project_number" varchar(100),
	"shipowner_orderer" varchar(200),
	"governing_law" varchar(100),
	"project_type" varchar(100),
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "legal_work_responses" (
	"id" serial PRIMARY KEY NOT NULL,
	"legal_work_id" integer NOT NULL,
	"response_content" text NOT NULL,
	"response_reviewer" varchar(100),
	"response_confirmer" varchar(100),
	"response_approver" varchar(100),
	"reviewed_at" timestamp,
	"confirmed_at" timestamp,
	"approved_at" timestamp,
	"is_public" boolean DEFAULT false NOT NULL,
	"is_re_revision" boolean DEFAULT false NOT NULL,
	"parent_response_id" integer,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "legal_works" (
	"id" serial PRIMARY KEY NOT NULL,
	"category" varchar(50) NOT NULL,
	"status" varchar(100) NOT NULL,
	"company_id" integer,
	"vendor_code" varchar(50) NOT NULL,
	"vendor_name" varchar(200) NOT NULL,
	"is_urgent" boolean DEFAULT false NOT NULL,
	"request_date" date,
	"consultation_date" date,
	"expected_answer_date" date,
	"legal_completion_date" date,
	"reviewer" varchar(100),
	"legal_responder" varchar(100),
	"has_attachment" boolean DEFAULT false NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "legal_work_attachments" ADD CONSTRAINT "legal_work_attachments_legal_work_id_legal_works_id_fk" FOREIGN KEY ("legal_work_id") REFERENCES "public"."legal_works"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "legal_work_requests" ADD CONSTRAINT "legal_work_requests_legal_work_id_legal_works_id_fk" FOREIGN KEY ("legal_work_id") REFERENCES "public"."legal_works"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "legal_work_responses" ADD CONSTRAINT "legal_work_responses_legal_work_id_legal_works_id_fk" FOREIGN KEY ("legal_work_id") REFERENCES "public"."legal_works"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "legal_works" ADD CONSTRAINT "legal_works_company_id_vendors_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."vendors"("id") ON DELETE set null ON UPDATE no action;