# Copyright (C) 2015 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: andraz@reciprocitylabs.com
# Maintained By: urban@reciprocitylabs.com

"""Add Comments

Revision ID: 4cd52e0a17b8
Revises: 3c8f204ba7a9
Create Date: 2015-10-23 13:00:18.555497

"""

# revision identifiers, used by Alembic.
revision = '4cd52e0a17b8'
down_revision = '27684e5f313a'

from alembic import op
import sqlalchemy as sa


from ggrc import db
from ggrc.models import Comment
from ggrc.models import DocumentationResponse
from ggrc.models import InterviewResponse
from ggrc.models import Relationship


def upgrade():
  op.create_table(
      'comments',
      sa.Column('id', sa.Integer(), primary_key=True, nullable=False),
      sa.Column('description', sa.Text()),
      sa.Column('created_at', sa.DateTime()),
      sa.Column('modified_by_id', sa.Integer()),
      sa.Column('updated_at', sa.DateTime()),
      sa.Column('context_id', sa.Integer(), sa.ForeignKey('contexts.id'))
  )

  documentation_responses = db.session.query(DocumentationResponse)
  for dr in documentation_responses:
    related = dr.related_sources + dr.related_destinations
    comment = Comment(
      description = dr.description,
      created_at = dr.created_at,
      modified_by = dr.modified_by,
      updated_at = dr.updated_at,
      context = dr.context)

    request_comment_rel = Relationship(
      source=dr.request,
      destination=comment)

    for rel in related:
      if rel.source.type == "DocumentationResponse":
        destination = rel.destination
      elif rel.destination.type == "DocumentationResponse":
        destination = rel.source
      related_objects_to_request = Relationship(
        source=dr.request,
        destination=destination
      )
      db.session.add(related_objects_to_request)
    db.session.add(comment)
    db.session.add(request_comment_rel)
  db.session.commit()

  interview_responses = db.session.query(InterviewResponse)
  for ir  in interview_responses:
    related = ir.related_sources + ir.related_destinations

    desc = ir.description
    if desc:
      desc += " "

    if ir.meetings:
      for m in ir.meetings:
        desc +=  ("Meeting requested on {}.").format(
          m.created_at.strftime("%m/%d/%Y at %H:%M"))
        if m.people:
          people = ", ".join(["{} ({})".format(
            p.name, p.email) for p in m.people])
          desc = desc[:-1] + " with following attendees: {}. ".format(people)

    comment = Comment(
      description = desc,
      created_at = ir.created_at,
      modified_by = ir.modified_by,
      updated_at = ir.updated_at,
      context = ir.context)

    request_comment_rel = Relationship(
      source=ir.request,
      destination=comment)

    for rel in related:
      if rel.source.type == "InterviewResponse":
        destination = rel.destination
      elif rel.destination.type == "InterviewResponse":
        destination = rel.source
      related_objects_to_request = Relationship(
        source=ir.request,
        destination=destination
      )
      db.session.add(related_objects_to_request)
    db.session.add(comment)
    db.session.add(request_comment_rel)
  db.session.commit()


def downgrade():
  op.drop_table('comments')

@@ -2160,20 +2160,27 @@
 %0A    if 
-desc
+ir.meetings
 :%0A      
@@ -2192,31 +2192,43 @@
 += %22
- %22%0A%0A    if ir.meetings:
+%3Cbr /%3E%3Cbr /%3E%3Cb%3EMeetings%3C/b%3E%3Chr /%3E%22%0A
 %0A   
@@ -2272,18 +2272,38 @@
  += 
- (%22
+%22%3Ca href=%5C%22%7Burl%7D%5C%22%3E
 Meeting
+%3C/a%3E
  req
@@ -2317,20 +2317,43 @@
 on %7B
-%7D.%22).
+date%7D%3Cbr /%3E%22.%5C%0A          
 format(
+**%7B
 %0A   
@@ -2358,16 +2358,58 @@
         
+     %22url%22: m.title,%0A              %22date%22:
  m.creat
@@ -2447,22 +2447,35 @@
 %25M%22)
-)
+,
 %0A    
+        %7D)%0A%0A
     if 
-m
+ir
 .peo
@@ -2489,78 +2489,58 @@
     
-    people = %22, %22.join(%5B%22%7B%7D (%7B%7D)%22.format(%0A            p.name, p.email)
+desc += %22%3Cbr /%3E%3Cbr /%3E%3Cb%3EAttendees%3C/b%3E%3Chr /%3E%22%0A     
  for
@@ -2549,18 +2549,18 @@
  in 
-m
+ir
 .people
-%5D)
+:
 %0A   
@@ -2568,76 +2568,56 @@
     
-  
 desc 
-= desc%5B:-1%5D + %22 with following attendees: %7B%7D. %22.format(people
++= %22- %7B%7D (%7B%7D)%3Cbr /%3E%22.format(p.name, p.email
 )%0A%0A 

Convert Interview response to a descriptive comment