import datetime
import uuid

from django.db import models

from user.models import User


class Setting(models.Model):
    pre_booking_interval_day = models.IntegerField('提前预约时间间隔（天）', default=30)
    booking_interval = models.IntegerField('预约间隔（分钟）', default=30)
    start_time = models.TimeField('开始时间', default=datetime.time(8, 0))
    end_time = models.TimeField('结束时间', default=datetime.time(21, 0))
    punish_point = models.IntegerField('惩罚积分数', default=5)
    reward_point = models.IntegerField('奖励积分数', default=10)

    class Meta:
        verbose_name = '预约设置'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.start_time}-{self.end_time}'


class Room(models.Model):
    id = models.CharField('ID', max_length=36, default=uuid.uuid1, primary_key=True)
    name = models.CharField('名称', max_length=128, blank=True)
    description = models.TextField('说明', max_length=512, blank=True)

    class Meta:
        verbose_name = '房间'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.name}'


class Seat(models.Model):
    id = models.CharField('ID', max_length=36, default=uuid.uuid1, primary_key=True)
    name = models.CharField('名称', max_length=128, blank=True)
    room = models.ForeignKey(verbose_name='房间', to=Room, on_delete=models.CASCADE)
    created_datetime = models.DateTimeField('创建时间', auto_now_add=True)

    class Meta:
        verbose_name = '座位'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.room.id}-{self.room.name}-{self.id}'


class Booking(models.Model):
    STATUS_CHIOCE = [
        ('PENDING', '正在申请'),
        ('SUCCESS', '申请成功'),
        ('FAILED', '申请失败'),
        ('CANCELED', '被取消'),
    ]
    user = models.ForeignKey(verbose_name='预约人', to=User, on_delete=models.CASCADE, related_name='user')
    room = models.ForeignKey(verbose_name='房间', to=Room, on_delete=models.CASCADE)
    seats = models.ManyToManyField(verbose_name='多个座位', to=Seat, related_name='seats')
    created_datetime = models.DateTimeField('预约日期时间', auto_now_add=True)
    start_time = models.TimeField('开始时间')
    end_time = models.TimeField('结束时间')
    date = models.DateField('日期')
    arrive_time = models.TimeField('到达时间', blank=True, null=True)
    leave_time = models.TimeField('离开时间', blank=True, null=True)
    status = models.CharField('状态', choices=STATUS_CHIOCE, max_length=10, default='PENDING')
    cancel_datetime = models.DateTimeField('取消日期时间', blank=True, null=True)
    cancel_reason = models.CharField('取消理由', max_length=512, blank=True, null=True)
    cancel_by = models.ForeignKey(verbose_name='取消人', to=User, on_delete=models.CASCADE, related_name='cancel_by',
                                  blank=True, null=True)

    class Meta:
        verbose_name = '预约'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.username}-{self.date}'
