# Request Summary Feature - Database Design

## Overview
This feature allows initiators to create and share comprehensive summaries of closed requests with other users.

## Database Schema

### 1. `request_summaries` Table
Stores the summary data for a closed request.

```sql
CREATE TABLE request_summaries (
    summary_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    request_id UUID NOT NULL REFERENCES workflow_requests(request_id) ON DELETE CASCADE,
    initiator_id UUID NOT NULL REFERENCES users(user_id) ON DELETE CASCADE,
    title VARCHAR(500) NOT NULL, -- Request title
    description TEXT, -- Request description
    closing_remarks TEXT, -- Final conclusion remarks (from conclusion_remarks or manual)
    is_ai_generated BOOLEAN DEFAULT false, -- Whether closing remarks are AI-generated
    conclusion_id UUID REFERENCES conclusion_remarks(conclusion_id) ON DELETE SET NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    
    CONSTRAINT uk_request_summary UNIQUE (request_id)
);

CREATE INDEX idx_request_summaries_request_id ON request_summaries(request_id);
CREATE INDEX idx_request_summaries_initiator_id ON request_summaries(initiator_id);
CREATE INDEX idx_request_summaries_created_at ON request_summaries(created_at);
```

### 2. `shared_summaries` Table
Stores sharing relationships - who shared which summary with whom.

```sql
CREATE TABLE shared_summaries (
    shared_summary_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    summary_id UUID NOT NULL REFERENCES request_summaries(summary_id) ON DELETE CASCADE,
    shared_by UUID NOT NULL REFERENCES users(user_id) ON DELETE CASCADE, -- Who shared it
    shared_with UUID NOT NULL REFERENCES users(user_id) ON DELETE CASCADE, -- Who can view it
    shared_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    viewed_at TIMESTAMP, -- When the recipient viewed it
    is_read BOOLEAN DEFAULT false, -- Whether recipient has viewed it
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    
    CONSTRAINT uk_shared_summary UNIQUE (summary_id, shared_with) -- Prevent duplicate shares
);

CREATE INDEX idx_shared_summaries_summary_id ON shared_summaries(summary_id);
CREATE INDEX idx_shared_summaries_shared_by ON shared_summaries(shared_by);
CREATE INDEX idx_shared_summaries_shared_with ON shared_summaries(shared_with);
CREATE INDEX idx_shared_summaries_shared_at ON shared_summaries(shared_at);
```

## Data Flow

1. **Request Closure**: When a request is closed, the initiator can create a summary
2. **Summary Creation**: 
   - Pulls data from `workflow_requests`, `approval_levels`, and `conclusion_remarks`
   - Creates a record in `request_summaries`
3. **Sharing**: 
   - Initiator selects users to share with
   - Creates records in `shared_summaries` for each recipient
4. **Viewing**: 
   - Users see shared summaries in "Shared Summary" menu
   - When viewed, `viewed_at` and `is_read` are updated

## Summary Content Structure

The summary will contain:
- **Request Information**: Request number, title, description
- **Initiator Details**: Name, designation, status, timestamp, remarks
- **Approver Details** (for each level): Name, designation, status, timestamp, remarks
- **Closing Remarks**: Final conclusion (AI-generated or manual)

## API Endpoints

### Summary Management
- `POST /api/v1/summaries` - Create summary for a closed request
- `GET /api/v1/summaries/:summaryId` - Get summary details
- `POST /api/v1/summaries/:summaryId/share` - Share summary with users
- `DELETE /api/v1/summaries/:summaryId/share/:userId` - Unshare summary

### Shared Summaries (for recipients)
- `GET /api/v1/summaries/shared` - List summaries shared with current user
- `GET /api/v1/summaries/shared/:sharedSummaryId` - Get shared summary details
- `PATCH /api/v1/summaries/shared/:sharedSummaryId/view` - Mark as viewed

### My Summaries (for initiators)
- `GET /api/v1/summaries/my` - List summaries created by current user

