-- -----------------------------------------------------------------------------
-- Roles
-- -----------------------------------------------------------------------------

INSERT INTO roles (code, name, description, is_system_role) VALUES
  ('admin', 'Admin', 'Full tenant administration access', 1),
  ('partner', 'Partner', 'Firm leadership with strategic and approval access', 1),
  ('attorney', 'Attorney', 'Case owner and legal reviewer', 1),
  ('paralegal', 'Paralegal', 'Operational case preparation and document management', 1),
  ('intake', 'Intake', 'Lead intake and client onboarding', 1),
  ('assistant', 'Assistant', 'Administrative support and coordination', 1),
  ('reviewer', 'Reviewer', 'Quality control and legal review', 1);

-- -----------------------------------------------------------------------------
-- Permissions
-- -----------------------------------------------------------------------------

INSERT INTO permissions (code, module_name, name, description) VALUES
  ('tenant.manage', 'tenant', 'Manage tenant', 'Manage law firm level settings and billing'),
  ('users.manage', 'users', 'Manage users', 'Invite users, assign roles and deactivate access'),
  ('clients.read', 'clients', 'Read clients', 'Read client profiles and linked data'),
  ('clients.write', 'clients', 'Write clients', 'Create and update clients and related parties'),
  ('cases.read', 'cases', 'Read cases', 'Read cases, timelines and workflow state'),
  ('cases.write', 'cases', 'Write cases', 'Create and update cases and assignments'),
  ('documents.manage', 'documents', 'Manage documents', 'Upload, classify and review documents'),
  ('workflows.manage', 'workflows', 'Manage workflows', 'Create workflow templates and activate them'),
  ('questionnaires.manage', 'questionnaires', 'Manage questionnaires', 'Launch and review intelligent questionnaires'),
  ('ai.execute', 'ai', 'Execute AI', 'Run AI extraction, gap analysis and form filling'),
  ('ai.review', 'ai', 'Review AI', 'Review AI outputs and confirm facts'),
  ('packets.manage', 'packets', 'Manage packets', 'Assemble and finalize filing packets'),
  ('audit.read', 'audit', 'Read audit', 'Read audit logs and review history');

INSERT INTO role_permissions (role_code, permission_code) VALUES
  ('admin', 'tenant.manage'),
  ('admin', 'users.manage'),
  ('admin', 'clients.read'),
  ('admin', 'clients.write'),
  ('admin', 'cases.read'),
  ('admin', 'cases.write'),
  ('admin', 'documents.manage'),
  ('admin', 'workflows.manage'),
  ('admin', 'questionnaires.manage'),
  ('admin', 'ai.execute'),
  ('admin', 'ai.review'),
  ('admin', 'packets.manage'),
  ('admin', 'audit.read'),
  ('partner', 'clients.read'),
  ('partner', 'cases.read'),
  ('partner', 'documents.manage'),
  ('partner', 'workflows.manage'),
  ('partner', 'questionnaires.manage'),
  ('partner', 'ai.execute'),
  ('partner', 'ai.review'),
  ('partner', 'packets.manage'),
  ('partner', 'audit.read'),
  ('attorney', 'clients.read'),
  ('attorney', 'clients.write'),
  ('attorney', 'cases.read'),
  ('attorney', 'cases.write'),
  ('attorney', 'documents.manage'),
  ('attorney', 'questionnaires.manage'),
  ('attorney', 'ai.execute'),
  ('attorney', 'ai.review'),
  ('attorney', 'packets.manage'),
  ('paralegal', 'clients.read'),
  ('paralegal', 'clients.write'),
  ('paralegal', 'cases.read'),
  ('paralegal', 'cases.write'),
  ('paralegal', 'documents.manage'),
  ('paralegal', 'questionnaires.manage'),
  ('paralegal', 'ai.execute'),
  ('paralegal', 'packets.manage'),
  ('intake', 'clients.read'),
  ('intake', 'clients.write'),
  ('intake', 'cases.read'),
  ('intake', 'cases.write'),
  ('intake', 'questionnaires.manage'),
  ('assistant', 'clients.read'),
  ('assistant', 'cases.read'),
  ('assistant', 'documents.manage'),
  ('reviewer', 'clients.read'),
  ('reviewer', 'cases.read'),
  ('reviewer', 'documents.manage'),
  ('reviewer', 'ai.review'),
  ('reviewer', 'audit.read');

-- -----------------------------------------------------------------------------
-- Core statuses
-- -----------------------------------------------------------------------------

INSERT INTO case_statuses (code, name, description, sort_order, is_closed_status) VALUES
  ('intake', 'Intake', 'Case is being qualified and structured', 10, 0),
  ('collecting_documents', 'Collecting Documents', 'Required evidence is still being collected', 20, 0),
  ('questionnaire_pending', 'Questionnaire Pending', 'Awaiting client responses to missing information', 30, 0),
  ('drafting', 'Drafting', 'Forms and packet are being prepared', 40, 0),
  ('attorney_review', 'Attorney Review', 'Case is under legal review', 50, 0),
  ('ready_to_file', 'Ready To File', 'Case is complete and ready for submission', 60, 0),
  ('filed', 'Filed', 'Case was filed with the target authority', 70, 0),
  ('closed', 'Closed', 'Case was completed or archived', 999, 1);

INSERT INTO questionnaire_statuses (code, name, description, is_terminal) VALUES
  ('draft', 'Draft', 'Session created but not sent yet', 0),
  ('issued', 'Issued', 'Link created and waiting for client access', 0),
  ('active', 'Active', 'Client is answering or the system is re-evaluating gaps', 0),
  ('waiting_re_evaluation', 'Waiting Re-Evaluation', 'Answer received and follow-up analysis pending', 0),
  ('completed', 'Completed', 'All mandatory gaps were resolved', 1),
  ('expired', 'Expired', 'Link/session expired', 1),
  ('cancelled', 'Cancelled', 'Session intentionally closed', 1);

-- -----------------------------------------------------------------------------
-- Repository item types
-- -----------------------------------------------------------------------------

INSERT INTO repository_item_types (code, name, description, supports_file, supports_text) VALUES
  ('email', 'Email', 'Inbound or outbound email', 0, 1),
  ('phone_call', 'Phone Call', 'Phone call summary or transcript', 0, 1),
  ('whatsapp_message', 'WhatsApp Message', 'WhatsApp conversation message', 0, 1),
  ('form_submission', 'Form Submission', 'Submitted intake or portal form', 0, 1),
  ('document', 'Document', 'Uploaded document or evidence file', 1, 1),
  ('image', 'Image', 'Image file linked to the case', 1, 0),
  ('audio', 'Audio', 'Audio file or recording', 1, 0),
  ('video', 'Video', 'Video file', 1, 0),
  ('note', 'Note', 'Internal note', 0, 1),
  ('extracted_fact', 'Extracted Fact', 'Structured fact derived from sources', 0, 1),
  ('client_profile_summary', 'Client Profile Summary', 'AI-synthesized client knowledge summary for downstream form filling', 0, 1),
  ('generated_form', 'Generated Form', 'AI or system-generated filled form', 1, 1),
  ('final_packet', 'Final Packet', 'Compiled final filing packet', 1, 1),
  ('questionnaire_answer', 'Questionnaire Answer', 'Client answer from intelligent questionnaire', 0, 1),
  ('ai_gap_analysis', 'AI Gap Analysis', 'Gap analysis run artifact', 0, 1),
  ('client_portal_chat', 'Client Portal Chat', 'Portal chat message', 0, 1);

-- -----------------------------------------------------------------------------
-- Channels
-- -----------------------------------------------------------------------------

INSERT INTO channels (code, name, description, is_external) VALUES
  ('email', 'Email', 'Email channel', 1),
  ('phone', 'Phone', 'Phone calls and voice interactions', 1),
  ('whatsapp', 'WhatsApp', 'WhatsApp messages', 1),
  ('web_form', 'Web Form', 'Website or intake form submission', 1),
  ('portal', 'Client Portal', 'Secure client portal interaction', 1),
  ('internal', 'Internal', 'Internal staff-only interaction', 0),
  ('ocr', 'OCR Pipeline', 'Automated OCR and extraction pipeline', 0),
  ('system', 'System', 'Automated system generated event', 0);

-- -----------------------------------------------------------------------------
-- Document types
-- -----------------------------------------------------------------------------

INSERT INTO document_types (code, name, description, category_code, is_identity_document, is_expirable) VALUES
  ('passport', 'Passport', 'Passport identity document', 'identity', 1, 1),
  ('birth_certificate', 'Birth Certificate', 'Civil birth certificate', 'civil_record', 1, 0),
  ('marriage_certificate', 'Marriage Certificate', 'Civil marriage certificate', 'civil_record', 0, 0),
  ('resume', 'Resume', 'Professional resume or CV', 'professional', 0, 0),
  ('employment_letter', 'Employment Letter', 'Offer letter or employment verification', 'employment', 0, 0),
  ('pay_stub', 'Pay Stub', 'Proof of current compensation', 'financial', 0, 0),
  ('tax_return', 'Tax Return', 'Income tax filing evidence', 'financial', 0, 0),
  ('visa', 'Visa', 'Visa page or approval evidence', 'immigration', 1, 1),
  ('i94', 'I-94', 'Admission record', 'immigration', 0, 0),
  ('photo', 'Photo', 'Passport-style or evidence photo', 'media', 0, 0),
  ('translation', 'Translation', 'Certified translation document', 'supporting', 0, 0),
  ('other_supporting', 'Other Supporting', 'Any additional supporting evidence', 'supporting', 0, 0);

-- -----------------------------------------------------------------------------
-- AI run types
-- -----------------------------------------------------------------------------

INSERT INTO ai_run_types (code, name, description, billable_default) VALUES
  ('extraction', 'Extraction', 'Extract structured data from documents or interactions', 1),
  ('summarization', 'Summarization', 'Summarize long-form content', 1),
  ('classification', 'Classification', 'Categorize content or case artifacts', 1),
  ('fact_consolidation', 'Fact Consolidation', 'Merge evidence into normalized facts', 1),
  ('gap_analysis', 'Gap Analysis', 'Identify missing, conflicting or low-confidence data', 1),
  ('workflow_blueprint_analysis', 'Workflow Blueprint Analysis', 'Analyze workflow templates and generate document checklists plus unique intake questions', 1),
  ('questionnaire_generation', 'Questionnaire Generation', 'Generate the next set of client questions', 1),
  ('questionnaire_re_evaluation', 'Questionnaire Re-Evaluation', 'Re-evaluate gaps after each answer', 1),
  ('form_fill', 'Form Fill', 'Fill forms from normalized facts', 1),
  ('packet_assembly', 'Packet Assembly', 'Assemble the final case packet', 1),
  ('pre_submission_analysis', 'Pre-Submission Analysis', 'Consultive AI analysis before document review submission', 1),
  ('submission_analysis', 'Submission Analysis', 'AI analysis executed during document review submission', 1),
  ('review_assist', 'Review Assist', 'Reviewer support analysis for a submitted document version', 1),
  ('final_review_validation', 'Final Review Validation', 'Mandatory AI validation before approving a reviewed document', 1),
  ('review_comment_reply', 'Review Comment Reply', 'AI response for anchored reviewer comments on document excerpts', 1),
  ('team_agent_assistant', 'Team Agent Assistant', 'Conversational query against a team member agent memory base', 1),
  ('workspace_global_assistant', 'Workspace Global Assistant', 'Conversational query against the consolidated workspace knowledge base', 1);
