# Narasi Ahli Backend Development Plan

## Overview
This document outlines the development plan for implementing the Narasi Ahli backend features based on the existing codebase architecture and requirements.

## Current Architecture Analysis

### Existing Structure
- **Framework**: Go with Fiber web framework
- **Database**: PostgreSQL with GORM ORM
- **Architecture**: Clean Architecture with modules containing:
  - Entity (Database models)
  - Repository (Data access layer)
  - Service (Business logic layer)
  - Controller (HTTP handlers)
  - Request/Response DTOs
  - Mapper (Entity ↔ DTO conversion)
- **Dependency Injection**: Uber FX
- **Middleware**: Audit trails, CSRF, Client authentication
- **Documentation**: Swagger/OpenAPI

### Current Users Entity Structure
The existing users entity already contains many required fields:
- ID, Username, Email, Fullname
- PhoneNumber, Address, WorkType, GenderType
- IdentityType, IdentityGroup, IdentityNumber
- DateOfBirth, LastEducation
- UserRoleId, UserLevelId, StatusId
- ProfilePicturePath, CreatedAt, UpdatedAt

## Development Plan

### Phase 1: Database Schema & Entity Updates

#### 1.1 Update Users Entity for Profile Requirements
**File**: `app/database/entity/users/users.entity.go`

**Required Fields to Add/Modify**:
- `Degree` (gelar) - string
- `WhatsappNumber` - string  
- `LastJobTitle` (pekerjaan terakhir) - string

**Changes Needed**:
```go
type Users struct {
    // ... existing fields ...
    Degree          *string `json:"degree" gorm:"type:varchar"`          // Gelar
    WhatsappNumber  *string `json:"whatsapp_number" gorm:"type:varchar"` // Nomor WhatsApp
    LastJobTitle    *string `json:"last_job_title" gorm:"type:varchar"`  // Pekerjaan terakhir
    // ... rest of existing fields ...
}
```

#### 1.2 Create New Entities

**1.2.1 Education History Entity**
**File**: `app/database/entity/education_history.entity.go`
```go
type EducationHistory struct {
    ID                uint      `json:"id" gorm:"primaryKey;type:int4;autoIncrement"`
    UserID            uint      `json:"user_id" gorm:"type:int4;not null"`
    SchoolName        string    `json:"school_name" gorm:"type:varchar;not null"`
    Major             string    `json:"major" gorm:"type:varchar;not null"`           // Jurusan
    EducationLevel    string    `json:"education_level" gorm:"type:varchar;not null"` // Tingkat pendidikan
    GraduationYear    int       `json:"graduation_year" gorm:"type:int4;not null"`   // Tahun lulus
    CertificateImage  *string   `json:"certificate_image" gorm:"type:varchar"`       // Ijazah image
    CreatedAt         time.Time `json:"created_at" gorm:"default:now()"`
    UpdatedAt         time.Time `json:"updated_at" gorm:"default:now()"`
    User              *users.Users `json:"user" gorm:"foreignKey:UserID;references:ID"`
}
```

**1.2.2 Work History Entity**
**File**: `app/database/entity/work_history.entity.go`
```go
type WorkHistory struct {
    ID          uint      `json:"id" gorm:"primaryKey;type:int4;autoIncrement"`
    UserID      uint      `json:"user_id" gorm:"type:int4;not null"`
    JobTitle    string    `json:"job_title" gorm:"type:varchar;not null"`    // Nama pekerjaan
    CompanyName string    `json:"company_name" gorm:"type:varchar;not null"` // Nama perusahaan
    StartDate   time.Time `json:"start_date" gorm:"not null"`                // Tanggal mulai
    EndDate     *time.Time `json:"end_date"`                                 // Tanggal selesai (nullable untuk pekerjaan saat ini)
    CreatedAt   time.Time `json:"created_at" gorm:"default:now()"`
    UpdatedAt   time.Time `json:"updated_at" gorm:"default:now()"`
    User        *users.Users `json:"user" gorm:"foreignKey:UserID;references:ID"`
}
```

**1.2.3 Research Journals Entity**
**File**: `app/database/entity/research_journals.entity.go`
```go
type ResearchJournals struct {
    ID            uint      `json:"id" gorm:"primaryKey;type:int4;autoIncrement"`
    UserID        uint      `json:"user_id" gorm:"type:int4;not null"`
    JournalTitle  string    `json:"journal_title" gorm:"type:varchar;not null"`  // Nama judul jurnal
    Publisher     string    `json:"publisher" gorm:"type:varchar;not null"`      // Tempat publish
    JournalURL    string    `json:"journal_url" gorm:"type:varchar;not null"`    // URL jurnal
    PublishedDate *time.Time `json:"published_date"`                            // Tanggal publikasi
    CreatedAt     time.Time `json:"created_at" gorm:"default:now()"`
    UpdatedAt     time.Time `json:"updated_at" gorm:"default:now()"`
    User          *users.Users `json:"user" gorm:"foreignKey:UserID;references:ID"`
}
```

**1.2.4 Communication/Chat Entities**
**File**: `app/database/entity/communications.entity.go`
```go
type Conversations struct {
    ID          uint      `json:"id" gorm:"primaryKey;type:int4;autoIncrement"`
    Participant1ID uint   `json:"participant1_id" gorm:"type:int4;not null"`
    Participant2ID uint   `json:"participant2_id" gorm:"type:int4;not null"`
    LastMessageAt  *time.Time `json:"last_message_at"`
    CreatedAt   time.Time `json:"created_at" gorm:"default:now()"`
    UpdatedAt   time.Time `json:"updated_at" gorm:"default:now()"`
    Participant1 *users.Users `json:"participant1" gorm:"foreignKey:Participant1ID;references:ID"`
    Participant2 *users.Users `json:"participant2" gorm:"foreignKey:Participant2ID;references:ID"`
}

type ChatMessages struct {
    ID             uint      `json:"id" gorm:"primaryKey;type:int4;autoIncrement"`
    ConversationID uint      `json:"conversation_id" gorm:"type:int4;not null"`
    SenderID       uint      `json:"sender_id" gorm:"type:int4;not null"`
    MessageText    *string   `json:"message_text" gorm:"type:text"`
    MessageType    string    `json:"message_type" gorm:"type:varchar;not null;default:'text'"` // text, image, file, audio
    FileURL        *string   `json:"file_url" gorm:"type:varchar"`                             // untuk file/image/audio
    FileName       *string   `json:"file_name" gorm:"type:varchar"`
    FileSize       *int64    `json:"file_size" gorm:"type:bigint"`
    IsRead         bool      `json:"is_read" gorm:"default:false"`
    CreatedAt      time.Time `json:"created_at" gorm:"default:now()"`
    Conversation   *Conversations `json:"conversation" gorm:"foreignKey:ConversationID;references:ID"`
    Sender         *users.Users `json:"sender" gorm:"foreignKey:SenderID;references:ID"`
}
```

**1.2.5 AI Chat Entities**
**File**: `app/database/entity/ai_chat.entity.go`
```go
type ChatSessions struct {
    ID            uint      `json:"id" gorm:"primaryKey;type:int4;autoIncrement"`
    AISessionID   string    `json:"ai_session_id" gorm:"type:varchar;not null;unique"`
    UserID        uint      `json:"user_id" gorm:"type:int4;not null"`
    AgentID       *string   `json:"agent_id" gorm:"type:varchar"`
    Title         string    `json:"title" gorm:"type:varchar;not null"`
    MessageCount  int       `json:"message_count" gorm:"type:int4;default:0"`
    Status        string    `json:"status" gorm:"type:varchar;default:'active'"` // active, archived, deleted
    CreatedAt     time.Time `json:"created_at" gorm:"default:now()"`
    UpdatedAt     time.Time `json:"updated_at" gorm:"default:now()"`
    User          *users.Users `json:"user" gorm:"foreignKey:UserID;references:ID"`
}

type ChatMessages struct {
    ID          uint      `json:"id" gorm:"primaryKey;type:int4;autoIncrement"`
    SessionID   uint      `json:"session_id" gorm:"type:int4;not null"`
    MessageType string    `json:"message_type" gorm:"type:varchar;not null"` // user, assistant
    Content     string    `json:"content" gorm:"type:text;not null"`
    CreatedAt   time.Time `json:"created_at" gorm:"default:now()"`
    Session     *ChatSessions `json:"session" gorm:"foreignKey:SessionID;references:ID"`
}
```

**1.2.6 AI Chat Logs Entity**
**File**: `app/database/entity/ai_chat_logs.entity.go`
```go
type AIChatLogs struct {
    ID           uint      `json:"id" gorm:"primaryKey;type:int4;autoIncrement"`
    SessionID    uint      `json:"session_id" gorm:"type:int4;not null"`
    UserID       uint      `json:"user_id" gorm:"type:int4;not null"`
    StartDate    time.Time `json:"start_date" gorm:"not null"`
    EndDate      *time.Time `json:"end_date"`
    TotalDuration int64    `json:"total_duration" gorm:"type:bigint"` // in seconds
    CreatedAt    time.Time `json:"created_at" gorm:"default:now()"`
    UpdatedAt    time.Time `json:"updated_at" gorm:"default:now()"`
    Session      *ChatSessions `json:"session" gorm:"foreignKey:SessionID;references:ID"`
    User         *users.Users `json:"user" gorm:"foreignKey:UserID;references:ID"`
}
```

### Phase 2: Module Implementation

Each module will follow the existing pattern with:
- **Module file** (dependency injection setup)
- **Controller** (HTTP handlers)
- **Service** (business logic)
- **Repository** (data access)
- **Request DTOs** (input validation)
- **Response DTOs** (output formatting)
- **Mapper** (entity ↔ DTO conversion)

#### 2.1 Education History Module
**Location**: `app/module/education_history/`

**API Endpoints**:
- `GET /education-history` - Get all user's education history
- `GET /education-history/:id` - Get specific education record
- `POST /education-history` - Create new education record
- `PUT /education-history/:id` - Update education record
- `DELETE /education-history/:id` - Delete education record
- `POST /education-history/:id/certificate` - Upload certificate image

#### 2.2 Work History Module
**Location**: `app/module/work_history/`

**API Endpoints**:
- `GET /work-history` - Get all user's work history
- `GET /work-history/:id` - Get specific work record
- `POST /work-history` - Create new work record
- `PUT /work-history/:id` - Update work record
- `DELETE /work-history/:id` - Delete work record

#### 2.3 Research Journals Module
**Location**: `app/module/research_journals/`

**API Endpoints**:
- `GET /research-journals` - Get all user's research journals
- `GET /research-journals/:id` - Get specific journal
- `POST /research-journals` - Create new journal record
- `PUT /research-journals/:id` - Update journal record
- `DELETE /research-journals/:id` - Delete journal record

#### 2.4 Communication Module
**Location**: `app/module/communications/`

**API Endpoints**:
- `GET /conversations` - Get user's conversations
- `GET /conversations/:id` - Get specific conversation
- `POST /conversations` - Start new conversation
- `GET /conversations/:id/messages` - Get conversation messages
- `POST /conversations/:id/messages` - Send message
- `POST /conversations/:id/messages/file` - Send file message
- `PUT /conversations/:id/messages/:messageId/read` - Mark message as read

#### 2.5 AI Chat Module
**Location**: `app/module/ai_chat/`

**API Endpoints**:
- `GET /ai-chat/sessions` - Get user's AI chat sessions
- `POST /ai-chat/sessions` - Create new AI chat session
- `GET /ai-chat/sessions/:id` - Get specific session
- `PUT /ai-chat/sessions/:id` - Update session (title, status)
- `DELETE /ai-chat/sessions/:id` - Delete session
- `GET /ai-chat/sessions/:id/messages` - Get session messages
- `POST /ai-chat/sessions/:id/messages` - Send message to AI
- `PUT /ai-chat/sessions/:id/archive` - Archive session

#### 2.6 AI Chat Logs Module
**Location**: `app/module/ai_chat_logs/`

**API Endpoints**:
- `GET /ai-chat-logs` - Get user's chat logs
- `GET /ai-chat-logs/:id` - Get specific log
- `POST /ai-chat-logs` - Create new log entry
- `PUT /ai-chat-logs/:id` - Update log (end session)

### Phase 3: Updated Users Module

#### 3.1 Update Users Request/Response DTOs
**Files to modify**:
- `app/module/users/request/users.request.go`
- `app/module/users/response/users.response.go`
- `app/module/users/mapper/users.mapper.go`

**New fields to handle**:
- Degree (gelar)
- WhatsappNumber (nomor whatsapp)
- LastJobTitle (pekerjaan terakhir)

### Phase 4: Database Migrations & Router Updates

#### 4.1 Update Database Models Registration
**File**: `app/database/index.database.go`

Add new entities to the `Models()` function:
```go
entity.EducationHistory{},
entity.WorkHistory{},
entity.ResearchJournals{},
entity.Conversations{},
entity.ChatMessages{},
entity.ChatSessions{},
entity.AIChatLogs{},
```

#### 4.2 Update Router Registration
**Files to modify**:
- `app/router/api.go` - Add new module routers
- `main.go` - Add new module imports and dependency injection

### Phase 5: Implementation Order

1. **Database Schema Updates** (Users entity modification)
2. **Education History Module** (Complete implementation)
3. **Work History Module** (Complete implementation)
4. **Research Journals Module** (Complete implementation)
5. **Communication Module** (Complete implementation)
6. **AI Chat Module** (Complete implementation)
7. **AI Chat Logs Module** (Complete implementation)
8. **Users Module Updates** (Profile API enhancements)
9. **Integration Testing** (All modules)
10. **Documentation Updates** (Swagger/API docs)

### Phase 6: Additional Considerations

#### 6.1 File Upload Handling
- Implement file upload service for certificate images
- Support for chat file attachments (images, documents, audio)
- Integration with existing MinIO storage

#### 6.2 Real-time Features
- WebSocket support for real-time chat
- Push notifications for new messages

#### 6.3 AI Integration
- External AI service integration
- API rate limiting for AI calls
- Response caching strategies

#### 6.4 Security & Validation
- File upload validation (size, type restrictions)
- Message content validation
- User authorization for accessing conversations

#### 6.5 Performance Optimization
- Database indexing for chat queries
- Pagination for message history
- Caching for frequently accessed data

## API Documentation Structure

Each module will include comprehensive Swagger documentation following the existing pattern:
- Endpoint descriptions
- Request/response schemas
- Authentication requirements
- Error response formats
- Example payloads

## Testing Strategy

- Unit tests for service layer
- Integration tests for API endpoints
- Database migration testing
- File upload testing
- Real-time communication testing

## Deployment Considerations

- Database migration scripts
- Environment-specific configurations
- MinIO bucket setup for file storage
- AI service endpoint configuration
- Performance monitoring setup

---

This plan provides a comprehensive roadmap for implementing all the Narasi Ahli backend requirements while maintaining consistency with the existing codebase architecture and patterns.
