package contract

import (
	"time"

	"github.com/google/uuid"
)

type PermissionResponse struct {
	ID          uuid.UUID `json:"id"`
	Code        string    `json:"code"`
	Action      string    `json:"action,omitempty"`
	Description *string   `json:"description,omitempty"`
}

type CreatePermissionRequest struct {
	Code        string  `json:"code"` // unique
	Description *string `json:"description,omitempty"`
}

type UpdatePermissionRequest struct {
	Code        *string `json:"code,omitempty"`
	Description *string `json:"description,omitempty"`
}

type ListPermissionsResponse struct {
	Permissions []PermissionResponse `json:"permissions"`
}

type RoleWithPermissionsResponse struct {
	ID          uuid.UUID            `json:"id"`
	Name        string               `json:"name"`
	Code        string               `json:"code"`
	Description *string              `json:"description,omitempty"`
	Permissions []PermissionResponse `json:"permissions"`
	CreatedAt   time.Time            `json:"created_at"`
	UpdatedAt   time.Time            `json:"updated_at"`
}

type CreateRoleRequest struct {
	Name            string   `json:"name"`
	Code            string   `json:"code"`
	Description     *string  `json:"description,omitempty"`
	PermissionCodes []string `json:"permission_codes,omitempty"`
}

type UpdateRoleRequest struct {
	Name            *string   `json:"name,omitempty"`
	Code            *string   `json:"code,omitempty"`
	Description     *string   `json:"description,omitempty"`
	PermissionCodes *[]string `json:"permission_codes,omitempty"`
}

type ListRolesResponse struct {
	Roles []RoleWithPermissionsResponse `json:"roles"`
}

// Module contracts
type ModuleResponse struct {
	ID   uuid.UUID `json:"id"`
	Name string    `json:"name"`
	Code string    `json:"code"`
}

type ModuleWithPermissionsResponse struct {
	Module      ModuleResponse       `json:"module"`
	Permissions []PermissionResponse `json:"permissions"`
}

type PermissionsGroupedResponse struct {
	Data []ModuleWithPermissionsResponse `json:"data"`
}

// New Role contracts for the required API
type ModulePermissionInput struct {
	Module  string   `json:"module" binding:"required"`
	Actions []string `json:"actions" binding:"required"`
}

type CreateOrUpdateRoleRequest struct {
	Name        string                  `json:"name" binding:"required"`
	Code        string                  `json:"code" binding:"required"`
	Description string                  `json:"description,omitempty"`
	Permissions []ModulePermissionInput `json:"permissions" binding:"required"`
}

type RoleDetailResponse struct {
	ID          uuid.UUID                      `json:"id"`
	Name        string                         `json:"name"`
	Code        string                         `json:"code"`
	Description string                         `json:"description,omitempty"`
	CreatedAt   time.Time                      `json:"created_at"`
	UpdatedAt   time.Time                      `json:"updated_at"`
	Permissions []RolePermissionModuleResponse `json:"permissions"`
}

type RolePermissionModuleResponse struct {
	Module  ModuleResponse             `json:"module"`
	Actions []PermissionActionResponse `json:"actions"`
}

type PermissionActionResponse struct {
	ID          uuid.UUID `json:"id"`
	Action      string    `json:"action"`
	Code        string    `json:"code"`
	Description string    `json:"description,omitempty"`
}
