package models

import (
	"apskel-pos-be/internal/constants"
	"time"

	"github.com/google/uuid"
)

type Table struct {
	ID             uuid.UUID
	OrganizationID uuid.UUID
	OutletID       uuid.UUID
	TableName      string
	StartTime      *time.Time
	Status         constants.TableStatus
	OrderID        *uuid.UUID
	PaymentAmount  float64
	PositionX      float64
	PositionY      float64
	Capacity       int
	IsActive       bool
	Metadata       map[string]interface{}
	CreatedAt      time.Time
	UpdatedAt      time.Time
}

type CreateTableRequest struct {
	OutletID  uuid.UUID `validate:"required"`
	TableName string    `validate:"required,max=100"`
	PositionX float64   `validate:"required"`
	PositionY float64   `validate:"required"`
	Capacity  int       `validate:"required,min=1,max=20"`
	Metadata  map[string]interface{}
}

type UpdateTableRequest struct {
	TableName *string                `validate:"omitempty,max=100"`
	Status    *constants.TableStatus `validate:"omitempty"`
	PositionX *float64               `validate:"omitempty"`
	PositionY *float64               `validate:"omitempty"`
	Capacity  *int                   `validate:"omitempty,min=1,max=20"`
	IsActive  *bool                  `validate:"omitempty"`
	Metadata  map[string]interface{}
}

type OccupyTableRequest struct {
	OrderID   uuid.UUID `validate:"required"`
	StartTime time.Time `validate:"required"`
}

type ReleaseTableRequest struct {
	PaymentAmount float64 `validate:"required,min=0"`
}

type TableResponse struct {
	ID             uuid.UUID
	OrganizationID uuid.UUID
	OutletID       uuid.UUID
	TableName      string
	StartTime      *time.Time
	Status         constants.TableStatus
	OrderID        *uuid.UUID
	PaymentAmount  float64
	PositionX      float64
	PositionY      float64
	Capacity       int
	IsActive       bool
	Metadata       map[string]interface{}
	CreatedAt      time.Time
	UpdatedAt      time.Time
	Order          *OrderResponse
}

type ListTablesRequest struct {
	OrganizationID *uuid.UUID
	OutletID       *uuid.UUID
	Status         *constants.TableStatus
	IsActive       *bool
	Search         string
	Page           int `validate:"required,min=1"`
	Limit          int `validate:"required,min=1,max=100"`
}

type ListTablesResponse struct {
	Tables     []TableResponse
	TotalCount int
	Page       int
	Limit      int
	TotalPages int
}

func (t *Table) IsAvailable() bool {
	return t.Status == constants.TableStatusAvailable
}

func (t *Table) IsOccupied() bool {
	return t.Status == constants.TableStatusOccupied
}

func (t *Table) CanBeOccupied() bool {
	return t.Status == constants.TableStatusAvailable || t.Status == constants.TableStatusCleaning
}
