-- 1. SETUP PROJECT & MEMBERSHIP
INSERT INTO projects (id, project_name, project_desc) 
VALUES (1, 'Phoenix System Overhaul', 'Modernizing the core banking middleware and data services.');

-- Assign users to project (SuperAdmin, Editor, Validador, Admin)
INSERT INTO project_members (project_id, user_id) VALUES (1, 1), (1, 2), (1, 3), (1, 4);

-- 2. CREATE RELATIONSHIP TYPES
INSERT INTO relationship_types (project_id, type_name, type_description, inverse_type_name) VALUES
(1, 'Depends On', 'Requirement cannot be completed without the target', 'Is Required By'),
(1, 'Relates To', 'General association', 'Relates To');

-- 3. INSERT REQUIREMENTS
-- Requirement A: Active (Regular)
INSERT INTO requirements (id, project_id, user_id, tag_id, status_id, req_name, req_desc, priority_id, version)
VALUES (101, 1, 2, 1, 2, 'API Rate Limiting', 'The system must limit requests to 100 per minute per API key.', 1, 1);

-- Requirement B: Draft stage
INSERT INTO requirements (id, project_id, user_id, tag_id, status_id, req_name, req_desc, priority_id, version)
VALUES (102, 1, 4, 2, 1, 'Dark Mode UI', 'User interface should support a high-contrast dark theme.', 2, 1);

-- 4. INSERT ACCEPTANCE CRITERIA
INSERT INTO acceptance_criteria (requirement_id, criteria_text, is_accepted, last_editor_id) VALUES
(101, 'Throttling returns 429 Too Many Requests status code.', TRUE, 3),
(101, 'Admin users can bypass rate limits for internal testing.', FALSE, 2),
(102, 'Contrast ratio meets WCAG 2.1 AA standards.', FALSE, 4);

-- 5. SIMULATE HISTORY (Requirement A was edited once)
-- First, manually insert into history to represent the "Old" version 
INSERT INTO requirements_history (original_req_id, project_id, status_id, req_name, version, valid_from, valid_to, edited_by)
VALUES (101, 1, 1, 'API Rate Limiting (Draft)', 1, NOW() - INTERVAL '2 days', NOW() - INTERVAL '1 day', 2);

-- 6. GROUP ASSOCIATIONS
-- Linking "API Rate Limiting" to "Data Services" (id 2) and "Integration" (id 5)
INSERT INTO requirements_groups (requirement_id, group_id) VALUES (101, 2), (101, 5);

-- 7. LINKS & VALIDATIONS
INSERT INTO requirement_links (source_req_id, target_req_id, relationship_type_id, created_by)
VALUES (101, 102, 2, 1); -- API Relates to UI

INSERT INTO validations (requirement_id, user_id, status_id, req_version_snapshot, comment)
VALUES (101, 3, 1, 1, 'Logic looks sound, but check the Redis implementation for performance.');

-- 8. COMMENTS & REPLIES
INSERT INTO requirement_comments (id, requirement_id, user_id, comment_text)
VALUES (50, 101, 3, 'Do we need specific limits for different tiers of users?');

INSERT INTO requirement_comment_replies (parent_comment_id, user_id, reply_text)
VALUES (50, 2, 'Yes, the Premium tier should allow up to 500 requests per minute.');

-- Batch inserting 20 requirements for Project 1
INSERT INTO requirements (id, project_id, user_id, tag_id, status_id, req_name, req_desc, priority_id, version) VALUES
(201, 1, 2, 1, 2, 'User Authentication', 'System must support OAuth2 and OpenID Connect.', 1, 1),
(202, 1, 2, 1, 2, 'Password Reset Flow', 'Users must be able to reset passwords via email link.', 2, 1),
(203, 1, 4, 2, 2, 'Response Time Latency', 'API responses must be under 200ms for 95% of requests.', 1, 1),
(204, 1, 4, 2, 2, 'Concurrent Users', 'System must support 10,000 concurrent active sessions.', 1, 1),
(205, 1, 2, 3, 1, 'GDPR Compliance', 'General data protection regulation standards must be met.', 1, 1),
(206, 1, 2, 4, 2, 'Health Check Endpoint', 'A /health endpoint must return 200 OK status.', 3, 1),
(207, 1, 4, 5, 2, 'Data Encryption at Rest', 'All database disks must be encrypted using AES-256.', 1, 1),
(208, 1, 2, 6, 1, 'Legacy Bridge', 'Connectivity to the old mainframe via SOAP.', 3, 1),
(209, 1, 2, 1, 2, 'Search Functionality', 'Users can search for requirements by name or tag.', 2, 1),
(210, 1, 4, 2, 2, 'Mobile Responsiveness', 'The dashboard must work on screens down to 320px.', 2, 1),
(211, 1, 2, 1, 2, 'Export to PDF', 'Users can download a PDF summary of requirements.', 3, 1),
(212, 1, 4, 2, 2, 'System Uptime', 'The system shall maintain 99.9% availability.', 1, 1),
(213, 1, 2, 5, 2, 'Audit Log Retention', 'Logs must be kept for 7 years for compliance.', 2, 1),
(214, 1, 4, 1, 2, 'Batch Upload', 'Support for CSV upload of requirements.', 2, 1),
(215, 1, 2, 3, 1, 'Multi-language Support', 'Interface available in English, Spanish, and French.', 3, 1),
(216, 1, 4, 2, 2, 'Memory Usage Limit', 'Microservices should not exceed 512MB RAM.', 2, 1),
(217, 1, 2, 4, 2, 'API Documentation', 'Auto-generated Swagger/OpenAPI documentation.', 2, 1),
(218, 1, 4, 5, 2, 'Daily Backups', 'Automated backups every night at 02:00 AM.', 1, 1),
(219, 1, 2, 6, 1, 'Third-party SSO', 'Integration with Okta and Azure AD.', 2, 1),
(220, 1, 4, 1, 2, 'Soft Delete', 'Requirements should be marked as deleted before purging.', 2, 1);

-- Map some to groups
INSERT INTO requirements_groups (requirement_id, group_id) VALUES 
(201, 7), (203, 2), (207, 4), (210, 6), (218, 5);

INSERT INTO acceptance_criteria (requirement_id, criteria_text, is_accepted, last_editor_id) VALUES
(201, 'Login works with Google account.', TRUE, 1),
(201, 'Tokens expire after 1 hour.', FALSE, 2),
(203, 'Stress test shows <200ms with 5k users.', TRUE, 3),
(207, 'Verify encryption keys are rotated annually.', FALSE, 4);

-- Edit 1: Update Priority and Description of Requirement 201
UPDATE requirements 
SET priority_id = 1, 
    req_desc = 'System must support OAuth2, OpenID Connect, and SAML 2.0.', 
    last_editor_id = 1 
WHERE id = 201;

-- Edit 2: Change status of a Draft to Regular
UPDATE requirements 
SET status_id = 2, 
    last_editor_id = 2 
WHERE id = 205;

-- Edit 3: Change Acceptance Criteria (Triggers ac_history)
UPDATE acceptance_criteria 
SET is_accepted = TRUE, 
    last_editor_id = 3 
WHERE requirement_id = 201 AND criteria_text LIKE 'Tokens%';

-- Create a link first
INSERT INTO requirement_links (source_req_id, target_req_id, relationship_type_id, created_by)
VALUES (218, 207, 1, 1); -- Backups depend on Encryption

-- Delete a requirement (Requirement 220)
-- This will trigger the audit and clean up links/groups/criteria via CASCADE
DELETE FROM requirements WHERE id = 220;

-- Remove a group association (Requirement 210 from User Experience)
DELETE FROM requirements_groups WHERE requirement_id = 210 AND group_id = 6;