#!/bin/bash
set -e

echo "========================================"
echo " Seed Demo License Applications"
echo "========================================"
echo ""

# Check if running in Docker or locally
if [ -z "$DATABASE_HOST" ]; then
  # Load from .env file if not in Docker
  if [ -f ".env" ]; then
    set -a
    source .env
    set +a
  fi

  DATABASE_HOST=${DATABASE_HOST:-localhost}
  DATABASE_PORT=${DATABASE_PORT:-5432}
  DATABASE_NAME=${DATABASE_NAME:-goa_gel_platform}
  DATABASE_USER=${DATABASE_USER:-postgres}
  DATABASE_PASSWORD=${DATABASE_PASSWORD:-postgres}
fi

echo "Adding demo license applications..."

# Get citizen ID
CITIZEN_ID=$(PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -U "$DATABASE_USER" -d "$DATABASE_NAME" -tAc "SELECT id FROM applicants WHERE digilocker_id = 'DL-GOA-CITIZEN-001' LIMIT 1;" 2>/dev/null)

if [ -z "$CITIZEN_ID" ] || [ "$CITIZEN_ID" = "" ]; then
  echo "ERROR: Could not find citizen applicant. Run seeds first."
  exit 1
fi

CITIZEN_ID=$(echo $CITIZEN_ID | xargs)  # Trim whitespace

# Get workflow ID
WORKFLOW_ID=$(PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -U "$DATABASE_USER" -d "$DATABASE_NAME" -tAc "SELECT id FROM workflows WHERE workflow_type = 'RESORT_LICENSE' LIMIT 1;" 2>/dev/null)
WORKFLOW_ID=$(echo $WORKFLOW_ID | xargs)

# Get department IDs
FIRE_DEPT_ID=$(PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -U "$DATABASE_USER" -d "$DATABASE_NAME" -tAc "SELECT id FROM departments WHERE code = 'FIRE_DEPT' LIMIT 1;" 2>/dev/null)
FIRE_DEPT_ID=$(echo $FIRE_DEPT_ID | xargs)

TOURISM_DEPT_ID=$(PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -U "$DATABASE_USER" -d "$DATABASE_NAME" -tAc "SELECT id FROM departments WHERE code = 'TOURISM_DEPT' LIMIT 1;" 2>/dev/null)
TOURISM_DEPT_ID=$(echo $TOURISM_DEPT_ID | xargs)

echo "  - Found applicant: $CITIZEN_ID"
echo "  - Found workflow: $WORKFLOW_ID"

# Insert demo license applications
PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -U "$DATABASE_USER" -d "$DATABASE_NAME" <<EOF

-- Demo Application 1: Draft status
INSERT INTO license_requests (id, request_number, applicant_id, request_type, workflow_id, status, metadata, current_stage_id, created_at, updated_at)
VALUES (
  'demo1111-1111-1111-1111-111111111111',
  'GOA-2024-RESORT-001',
  '$CITIZEN_ID',
  'RESORT_LICENSE',
  '$WORKFLOW_ID',
  'DRAFT',
  '{"businessName": "Paradise Beach Resort", "location": "Calangute Beach", "type": "Beach Resort", "capacity": 50}',
  NULL,
  CURRENT_TIMESTAMP - INTERVAL '5 days',
  CURRENT_TIMESTAMP - INTERVAL '5 days'
) ON CONFLICT (request_number) DO NOTHING;

-- Demo Application 2: Pending - waiting for Fire Dept
INSERT INTO license_requests (id, request_number, applicant_id, request_type, workflow_id, status, metadata, current_stage_id, submitted_at, created_at, updated_at)
VALUES (
  'demo2222-2222-2222-2222-222222222222',
  'GOA-2024-RESORT-002',
  '$CITIZEN_ID',
  'RESORT_LICENSE',
  '$WORKFLOW_ID',
  'PENDING',
  '{"businessName": "Sunset View Resort", "location": "Baga Beach", "type": "Boutique Resort", "capacity": 30}',
  'stage_1_fire',
  CURRENT_TIMESTAMP - INTERVAL '3 days',
  CURRENT_TIMESTAMP - INTERVAL '4 days',
  CURRENT_TIMESTAMP - INTERVAL '3 days'
) ON CONFLICT (request_number) DO NOTHING;

-- Create pending approval for Application 2
INSERT INTO approvals (id, request_id, department_id, status, created_at, updated_at)
SELECT
  'appr2222-2222-2222-2222-222222222222',
  'demo2222-2222-2222-2222-222222222222',
  '$FIRE_DEPT_ID',
  'PENDING',
  CURRENT_TIMESTAMP - INTERVAL '3 days',
  CURRENT_TIMESTAMP - INTERVAL '3 days'
WHERE NOT EXISTS (
  SELECT 1 FROM approvals WHERE id = 'appr2222-2222-2222-2222-222222222222'
);

-- Create workflow state for Application 2
INSERT INTO workflow_states (id, request_id, current_stage_id, completed_stages, pending_approvals, execution_log, stage_started_at, created_at, updated_at)
SELECT
  'wfst2222-2222-2222-2222-222222222222',
  'demo2222-2222-2222-2222-222222222222',
  'stage_1_fire',
  '[]',
  '["$FIRE_DEPT_ID"]',
  '[{"action": "SUBMITTED", "timestamp": "' || (CURRENT_TIMESTAMP - INTERVAL '3 days')::text || '"}]',
  CURRENT_TIMESTAMP - INTERVAL '3 days',
  CURRENT_TIMESTAMP - INTERVAL '3 days',
  CURRENT_TIMESTAMP - INTERVAL '3 days'
WHERE NOT EXISTS (
  SELECT 1 FROM workflow_states WHERE request_id = 'demo2222-2222-2222-2222-222222222222'
);

-- Demo Application 3: In Review - Fire approved, Tourism/Municipality pending
INSERT INTO license_requests (id, request_number, applicant_id, request_type, workflow_id, status, metadata, current_stage_id, submitted_at, created_at, updated_at)
VALUES (
  'demo3333-3333-3333-3333-333333333333',
  'GOA-2024-RESORT-003',
  '$CITIZEN_ID',
  'RESORT_LICENSE',
  '$WORKFLOW_ID',
  'IN_REVIEW',
  '{"businessName": "Ocean Breeze Resort", "location": "Anjuna Beach", "type": "Eco Resort", "capacity": 25}',
  'stage_2_parallel',
  CURRENT_TIMESTAMP - INTERVAL '10 days',
  CURRENT_TIMESTAMP - INTERVAL '12 days',
  CURRENT_TIMESTAMP - INTERVAL '2 days'
) ON CONFLICT (request_number) DO NOTHING;

-- Fire approval for Application 3 (completed)
INSERT INTO approvals (id, request_id, department_id, status, remarks, created_at, updated_at)
SELECT
  'appr3333-fire-3333-3333-333333333333',
  'demo3333-3333-3333-3333-333333333333',
  '$FIRE_DEPT_ID',
  'APPROVED',
  'Fire safety requirements met. All exits properly marked.',
  CURRENT_TIMESTAMP - INTERVAL '7 days',
  CURRENT_TIMESTAMP - INTERVAL '5 days'
WHERE NOT EXISTS (
  SELECT 1 FROM approvals WHERE id = 'appr3333-fire-3333-3333-333333333333'
);

-- Tourism pending approval for Application 3
INSERT INTO approvals (id, request_id, department_id, status, created_at, updated_at)
SELECT
  'appr3333-tour-3333-3333-333333333333',
  'demo3333-3333-3333-3333-333333333333',
  '$TOURISM_DEPT_ID',
  'PENDING',
  CURRENT_TIMESTAMP - INTERVAL '5 days',
  CURRENT_TIMESTAMP - INTERVAL '5 days'
WHERE NOT EXISTS (
  SELECT 1 FROM approvals WHERE id = 'appr3333-tour-3333-3333-333333333333'
);

-- Demo Application 4: Approved (completed all stages)
INSERT INTO license_requests (id, request_number, applicant_id, request_type, workflow_id, status, metadata, current_stage_id, submitted_at, approved_at, created_at, updated_at)
VALUES (
  'demo4444-4444-4444-4444-444444444444',
  'GOA-2024-RESORT-004',
  '$CITIZEN_ID',
  'RESORT_LICENSE',
  '$WORKFLOW_ID',
  'APPROVED',
  '{"businessName": "Golden Sands Resort", "location": "Candolim Beach", "type": "Luxury Resort", "capacity": 100}',
  NULL,
  CURRENT_TIMESTAMP - INTERVAL '30 days',
  CURRENT_TIMESTAMP - INTERVAL '5 days',
  CURRENT_TIMESTAMP - INTERVAL '35 days',
  CURRENT_TIMESTAMP - INTERVAL '5 days'
) ON CONFLICT (request_number) DO NOTHING;

-- Demo Application 5: Rejected
INSERT INTO license_requests (id, request_number, applicant_id, request_type, workflow_id, status, metadata, current_stage_id, submitted_at, created_at, updated_at)
VALUES (
  'demo5555-5555-5555-5555-555555555555',
  'GOA-2024-RESORT-005',
  '$CITIZEN_ID',
  'RESORT_LICENSE',
  '$WORKFLOW_ID',
  'REJECTED',
  '{"businessName": "Beach Shack Resort", "location": "Morjim Beach", "type": "Beach Shack", "capacity": 15}',
  'stage_1_fire',
  CURRENT_TIMESTAMP - INTERVAL '20 days',
  CURRENT_TIMESTAMP - INTERVAL '25 days',
  CURRENT_TIMESTAMP - INTERVAL '15 days'
) ON CONFLICT (request_number) DO NOTHING;

-- Rejected approval for Application 5
INSERT INTO approvals (id, request_id, department_id, status, remarks, created_at, updated_at)
SELECT
  'appr5555-fire-5555-5555-555555555555',
  'demo5555-5555-5555-5555-555555555555',
  '$FIRE_DEPT_ID',
  'REJECTED',
  'Fire safety requirements not met. Insufficient emergency exits.',
  CURRENT_TIMESTAMP - INTERVAL '18 days',
  CURRENT_TIMESTAMP - INTERVAL '15 days'
WHERE NOT EXISTS (
  SELECT 1 FROM approvals WHERE id = 'appr5555-fire-5555-5555-555555555555'
);

EOF

echo ""
echo "Demo applications created:"
echo "  - GOA-2024-RESORT-001: Draft"
echo "  - GOA-2024-RESORT-002: Pending (Fire Dept review)"
echo "  - GOA-2024-RESORT-003: In Review (Tourism/Municipality pending)"
echo "  - GOA-2024-RESORT-004: Approved"
echo "  - GOA-2024-RESORT-005: Rejected"
echo ""
echo "Done!"
