from .models import Review
import csv
revs = Review.objects.all()


def write_to_csv():
    with open('review_data_set.csv', 'w') as file:
        header_row = ['reviewer', 'location', 'review', 'star_rating', 'create_time', 'reply', 'reply_update_time']
        writer = csv.writer(file)
        writer.writerow(header_row)
        for rev in revs:
            review = rev.comment if rev.comment else 'NULL'
            star_rating = rev.star_rating
            create_time = rev.create_time
            reviewer = rev.reviewer_name
            location = rev.location.care_name
            reply = rev.reply.replied_text if rev.reply else 'NULL'
            reply_update_time = rev.reply.create_time if rev.reply else 'NULL'
            row = [reviewer, location, review, star_rating, create_time, reply, reply_update_time]
            writer.writerow(row)

